Skip to content

Commit ae4dc9a

Browse files
committed
SDL: use texture to improve performance
1 parent 14aef59 commit ae4dc9a

File tree

5 files changed

+50
-40
lines changed

5 files changed

+50
-40
lines changed

src/common/var.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,13 @@ void v_add(var_t *result, var_t *a, var_t *b) {
366366
char tmpsb[64];
367367

368368
if (a->type == V_STR && b->type == V_STR) {
369+
int length = strlen(a->v.p.ptr) + strlen(b->v.p.ptr);
369370
result->type = V_STR;
370-
result->v.p.ptr = malloc(strlen(a->v.p.ptr) +
371-
strlen(b->v.p.ptr) + 1);
371+
result->v.p.ptr = malloc(length + 1);
372372
strcpy(result->v.p.ptr, a->v.p.ptr);
373373
strcat(result->v.p.ptr, b->v.p.ptr);
374-
result->v.p.size = strlen(result->v.p.ptr) + 1;
374+
result->v.p.ptr[length] = '\0';
375+
result->v.p.size = length + 1;
375376
return;
376377
} else if (a->type == V_INT && b->type == V_INT) {
377378
result->type = V_INT;

src/platform/android/jni/display.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ bool Canvas::create(int w, int h) {
4747
return result;
4848
}
4949

50-
void Canvas::copy(Canvas *src, const MAPoint2d *dstPoint, const MARect *srcRect) {
51-
int destY = dstPoint->y;
50+
void Canvas::copy(Canvas *src, const MARect *srcRect, int destX, int destY) {
5251
int srcH = srcRect->height;
5352
if (srcRect->top + srcRect->height > src->_h) {
5453
srcH = src->_h - srcRect->top;
5554
}
5655
for (int y = 0; y < srcH && destY < _h; y++, destY++) {
5756
pixel_t *line = src->getLine(y + srcRect->top) + srcRect->left;
58-
pixel_t *dstLine = getLine(destY) + dstPoint->x;
57+
pixel_t *dstLine = getLine(destY) + destX;
5958
memcpy(dstLine, line, srcRect->width * sizeof(pixel_t));
6059
}
6160
}

src/platform/sdl/display.cpp

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@
1313

1414
extern common::Graphics *graphics;
1515

16+
#if defined(PIXELFORMAT_RGB565)
17+
#define PIXEL_FORMAT SDL_PIXELFORMAT_RGB565
18+
#else
19+
#define PIXEL_FORMAT SDL_PIXELFORMAT_RGB888
20+
#endif
21+
1622
//
1723
// Canvas implementation
1824
//
1925
Canvas::Canvas() :
20-
_canvas(NULL),
26+
_surface(NULL),
2127
_clip(NULL) {
2228
}
2329

2430
Canvas::~Canvas() {
25-
if (_canvas != NULL) {
26-
SDL_FreeSurface(_canvas);
31+
if (_surface != NULL) {
32+
SDL_FreeSurface(_surface);
2733
}
2834
delete _clip;
29-
_canvas = NULL;
35+
_surface = NULL;
3036
_clip = NULL;
3137
}
3238

@@ -36,43 +42,30 @@ bool Canvas::create(int w, int h) {
3642
_h = h;
3743
int bpp;
3844
Uint32 rmask, gmask, bmask, amask;
39-
#if defined(PIXELFORMAT_RGB565)
40-
Uint32 format = SDL_PIXELFORMAT_RGB565;
41-
#else
42-
Uint32 format = SDL_PIXELFORMAT_RGB888;
43-
#endif
44-
SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask);
45-
_canvas = SDL_CreateRGBSurface(0, w, h, bpp, rmask, gmask, bmask, amask);
46-
return _canvas != NULL;
47-
}
48-
49-
bool Canvas::create(int w, int h, SDL_Window *window) {
50-
logEntered();
51-
_w = w;
52-
_h = h;
53-
_canvas = SDL_GetWindowSurface(window);
54-
return _canvas != NULL;
45+
SDL_PixelFormatEnumToMasks(PIXEL_FORMAT, &bpp, &rmask, &gmask, &bmask, &amask);
46+
_surface = SDL_CreateRGBSurface(0, w, h, bpp, rmask, gmask, bmask, amask);
47+
return _surface != NULL;
5548
}
5649

57-
void Canvas::copy(Canvas *src, const MARect *srcRect, int dstx, int dsty) {
50+
void Canvas::copy(Canvas *src, const MARect *srcRect, int destX, int destY) {
5851
SDL_Rect srcrect;
5952
srcrect.x = srcRect->left;
6053
srcrect.y = srcRect->top;
6154
srcrect.w = srcRect->width;
6255
srcrect.h = srcRect->height;
6356

6457
SDL_Rect dstrect;
65-
dstrect.x = dstx;
66-
dstrect.y = dsty;
58+
dstrect.x = destX;
59+
dstrect.y = destY;
6760
dstrect.w = _w;
6861
dstrect.h = _h;
6962

70-
SDL_BlitSurface(src->_canvas, &srcrect, _canvas, &dstrect);
63+
SDL_BlitSurface(src->_surface, &srcrect, _surface, &dstrect);
7164
}
7265

73-
pixel_t *Canvas::getLine(int y) {
74-
pixel_t *pixels = (pixel_t *)_canvas->pixels;
75-
return pixels + (y * _w);
66+
pixel_t *Canvas::getLine(int y) {
67+
pixel_t *pixels = (pixel_t *)_surface->pixels;
68+
return pixels + (y * _w);
7669
}
7770

7871
void Canvas::setClip(int x, int y, int w, int h) {
@@ -92,21 +85,30 @@ void Canvas::setClip(int x, int y, int w, int h) {
9285
// Graphics implementation
9386
//
9487
Graphics::Graphics(SDL_Window *window) : common::Graphics(),
88+
_renderer(NULL),
89+
_texture(NULL),
9590
_window(window) {
9691
}
9792

9893
Graphics::~Graphics() {
9994
logEntered();
95+
SDL_DestroyTexture(_texture);
96+
SDL_DestroyRenderer(_renderer);
10097
}
10198

10299
bool Graphics::construct(const char *font, const char *boldFont) {
103100
logEntered();
104101

105-
SDL_GetWindowSize(_window, &_w, &_h);
106102
bool result = false;
107-
if (loadFonts(font, boldFont)) {
103+
SDL_GetWindowSize(_window, &_w, &_h);
104+
_renderer = SDL_CreateRenderer(_window, -1, 0);
105+
if (_renderer != NULL) {
106+
_texture = SDL_CreateTexture(_renderer, PIXEL_FORMAT,
107+
SDL_TEXTUREACCESS_STREAMING, _w, _h);
108+
}
109+
if (_texture != NULL && loadFonts(font, boldFont)) {
108110
_screen = new Canvas();
109-
if (_screen && _screen->create(getWidth(), getHeight(), _window)) {
111+
if (_screen && _screen->create(getWidth(), getHeight())) {
110112
_drawTarget = _screen;
111113
maSetColor(DEFAULT_BACKGROUND);
112114
result = true;
@@ -116,16 +118,23 @@ bool Graphics::construct(const char *font, const char *boldFont) {
116118
}
117119

118120
void Graphics::redraw() {
119-
SDL_UpdateWindowSurface(_window);
121+
SDL_UpdateTexture(_texture, NULL, _screen->_surface->pixels, _w * sizeof (pixel_t));
122+
SDL_RenderClear(_renderer);
123+
SDL_RenderCopy(_renderer, _texture, NULL, NULL);
124+
SDL_RenderPresent(_renderer);
120125
}
121126

122127
void Graphics::resize() {
123128
logEntered();
124129
bool drawScreen = (_drawTarget == _screen);
125130
delete _screen;
126131
_screen = new ::Canvas();
127-
_screen->create(getWidth(), getHeight(), _window);
132+
_screen->create(getWidth(), getHeight());
128133
_drawTarget = drawScreen ? _screen : NULL;
134+
135+
SDL_DestroyTexture(_texture);
136+
_texture = SDL_CreateTexture(_renderer, PIXEL_FORMAT,
137+
SDL_TEXTUREACCESS_STREAMING, _w, _h);
129138
}
130139

131140
bool Graphics::loadFonts(const char *font, const char *boldFont) {

src/platform/sdl/display.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct Graphics : common::Graphics {
2727
bool loadFonts(const char *font, const char *boldFont);
2828
bool loadFont(const char *filename, FT_Face &face);
2929

30+
SDL_Renderer *_renderer;
31+
SDL_Texture *_texture;
3032
SDL_Window *_window;
3133
};
3234

src/ui/canvas.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ struct Canvas {
2424
virtual ~Canvas();
2525

2626
bool create(int w, int h);
27-
bool create(int w, int h, SDL_Window *window);
2827
void copy(Canvas *src, const MARect *srcRect, int dstx, int dsty);
2928
void setClip(int x, int y, int w, int h);
3029
pixel_t *getLine(int y);
@@ -35,7 +34,7 @@ struct Canvas {
3534

3635
int _w;
3736
int _h;
38-
SDL_Surface *_canvas;
37+
SDL_Surface *_surface;
3938
SDL_Rect *_clip;
4039
};
4140

0 commit comments

Comments
 (0)