Skip to content

Commit 2a3c30a

Browse files
committed
SDL: minor performance changes
1 parent da96508 commit 2a3c30a

File tree

8 files changed

+52
-27
lines changed

8 files changed

+52
-27
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function buildSDL() {
218218
PACKAGE_LIBS="${PACKAGE_LIBS} ${FONTCONFIG_LIBS}"
219219
esac
220220

221-
PACKAGE_CFLAGS="${PACKAGE_CFLAGS} `sdl2-config --cflags` `freetype-config --cflags`"
221+
PACKAGE_CFLAGS="${PACKAGE_CFLAGS} `sdl2-config --cflags` `freetype-config --cflags` -fno-exceptions"
222222
case "${host_os}" in
223223
*mingw*)
224224
PACKAGE_LIBS="-Wl,-Bstatic ${PACKAGE_LIBS} `sdl2-config --static-libs` `freetype-config --libs`"

src/platform/android/jni/display.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ 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;
52+
int srcH = srcRect->height;
53+
if (srcRect->top + srcRect->height > src->_h) {
54+
srcH = src->_h - srcRect->top;
55+
}
56+
for (int y = 0; y < srcH && destY < _h; y++, destY++) {
57+
pixel_t *line = src->getLine(y + srcRect->top) + srcRect->left;
58+
pixel_t *dstLine = getLine(destY) + dstPoint->x;
59+
memcpy(dstLine, line, srcRect->width * sizeof(pixel_t));
60+
}
61+
}
62+
5063
void Canvas::setClip(int x, int y, int w, int h) {
5164
delete _clip;
5265
if (x != 0 || y != 0 || _w != w || _h != h) {

src/platform/android/jni/runtime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void Runtime::alert(const char *title, const char *message) {
189189
_app->activity->vm->DetachCurrentThread();
190190
}
191191

192-
int Runtime::ask(const char *title, const char *prompt, bool cancel) {,
192+
int Runtime::ask(const char *title, const char *prompt, bool cancel) {
193193
JNIEnv *env;
194194
_app->activity->vm->AttachCurrentThread(&env, NULL);
195195
jclass clazz = env->GetObjectClass(_app->activity->clazz);

src/platform/android/jni/runtime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct Runtime : public System {
2121
Runtime(android_app *app);
2222
virtual ~Runtime();
2323

24-
void alert(const char *title, const char *message, bool cancel);
25-
int ask(const char *title, const char *prompt);
24+
void alert(const char *title, const char *message);
25+
int ask(const char *title, const char *prompt, bool cancel);
2626
void clearSoundQueue();
2727
void construct();
2828
bool getUntrusted();

src/platform/sdl/display.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ bool Canvas::create(int w, int h) {
4646
return _canvas != NULL;
4747
}
4848

49+
void Canvas::copy(Canvas *src, const MARect *srcRect, int dstx, int dsty) {
50+
SDL_Rect srcrect;
51+
srcrect.x = srcRect->left;
52+
srcrect.y = srcRect->top;
53+
srcrect.w = srcRect->width;
54+
srcrect.h = srcRect->height;
55+
56+
SDL_Rect dstrect;
57+
dstrect.x = dstx;
58+
dstrect.y = dsty;
59+
dstrect.w = _w;
60+
dstrect.h = _h;
61+
62+
SDL_BlitSurface(src->_canvas, &srcrect, _canvas, &dstrect);
63+
}
64+
4965
pixel_t *Canvas::getLine(int y) {
5066
pixel_t *pixels = (pixel_t *)_canvas->pixels;
5167
return pixels + (y * _w);

src/ui/canvas.h

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

2626
bool create(int w, int h);
27+
void copy(Canvas *src, const MARect *srcRect, int dstx, int dsty);
2728
void setClip(int x, int y, int w, int h);
2829
pixel_t *getLine(int y);
2930
int x() { return _clip ? _clip->x : 0; }
@@ -46,6 +47,7 @@ struct Canvas {
4647
virtual ~Canvas();
4748

4849
bool create(int w, int h);
50+
void copy(Canvas *src, const MARect *srcRect, int dstx, int dsty);
4951
void setClip(int x, int y, int w, int h);
5052
pixel_t *getLine(int y) { return _canvas + (y * _w); }
5153
int x() { return _clip ? _clip->left : 0; }

src/ui/graphics.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ inline pixel_t RGB888_to_RGBA8888(unsigned c) {
6666
#define GET_FROM_RGB888(c) (c)
6767
#endif
6868

69-
#define GET_LINE(y) \
70-
pixel_t *line; \
71-
if (_cacheY == y) { \
69+
#define GET_LINE(line, y) \
70+
if (_cacheY == y && !_cacheLine) { \
7271
line = _cacheLine; \
7372
} else { \
7473
line = _drawTarget->getLine(y); \
7574
_cacheLine = line; \
76-
_cacheY = posY; \
75+
_cacheY = y; \
7776
}
7877

7978

@@ -146,10 +145,10 @@ Graphics::Graphics() :
146145
_screen(NULL),
147146
_drawTarget(NULL),
148147
_font(NULL),
149-
_w(0),
150-
_h(0),
148+
_cacheLine(NULL),
151149
_cacheY(-1),
152-
_cacheLine(NULL) {
150+
_w(0),
151+
_h(0) {
153152
graphics = this;
154153
}
155154

@@ -182,16 +181,7 @@ void Graphics::deleteFont(Font *font) {
182181

183182
void Graphics::drawImageRegion(Canvas *src, const MAPoint2d *dstPoint, const MARect *srcRect) {
184183
if (_drawTarget && _drawTarget != src) {
185-
int destY = dstPoint->y;
186-
int srcH = srcRect->height;
187-
if (srcRect->top + srcRect->height > src->_h) {
188-
srcH = src->_h - srcRect->top;
189-
}
190-
for (int y = 0; y < srcH && destY < _drawTarget->_h; y++, destY++) {
191-
pixel_t *line = src->getLine(y + srcRect->top) + srcRect->left;
192-
pixel_t *dstLine = _drawTarget->getLine(destY) + dstPoint->x;
193-
memcpy(dstLine, line, srcRect->width * sizeof(pixel_t));
194-
}
184+
_drawTarget->copy(src, srcRect, dstPoint->x, dstPoint->y);
195185
}
196186
}
197187

@@ -212,7 +202,8 @@ void Graphics::drawLine(int startX, int startY, int endX, int endY) {
212202
x2 = _w -1;
213203
}
214204
if (startY >= 0 && startY < _h) {
215-
pixel_t *line = _drawTarget->getLine(startY);
205+
pixel_t *line;
206+
GET_LINE(line, startY);
216207
for (int x = x1; x <= x2; x++) {
217208
if (x >= _drawTarget->x() && x < _drawTarget->w()) {
218209
line[x] = _drawColor;
@@ -256,7 +247,8 @@ void Graphics::drawPixel(int posX, int posY) {
256247
&& posY >= _drawTarget->y()
257248
&& posX < _drawTarget->w()
258249
&& posY < _drawTarget->h()) {
259-
GET_LINE(posY);
250+
pixel_t *line;
251+
GET_LINE(line, posY);
260252
line[posX] = _drawColor;
261253
}
262254
}
@@ -423,7 +415,8 @@ int Graphics::getPixel(Canvas *canvas, int posX, int posY) {
423415
&& posY > -1
424416
&& posX < canvas->_w
425417
&& posY < canvas->_h - 1) {
426-
pixel_t *line = canvas->getLine(posY);
418+
pixel_t *line;
419+
GET_LINE(line, posY);
427420
result = line[posX];
428421
}
429422
return result;
@@ -527,7 +520,8 @@ void Graphics::wuPlot(int posX, int posY, double c) {
527520
&& posY >= _drawTarget->y()
528521
&& posX < _drawTarget->w()
529522
&& posY < _drawTarget->h()) {
530-
pixel_t *line = _drawTarget->getLine(posY);
523+
pixel_t *line;
524+
GET_LINE(line, posY);
531525
uint8_t sR, sG, sB;
532526
uint8_t dR, dG, dB;
533527

src/ui/graphics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ struct Graphics {
7474
Canvas *_drawTarget;
7575
Font *_font;
7676
pixel_t _drawColor;
77-
int _w, _h;
78-
int _cacheY;
7977
pixel_t *_cacheLine;
78+
int _cacheY;
79+
int _w, _h;
8080
};
8181

8282
}

0 commit comments

Comments
 (0)