Skip to content

Commit 7781588

Browse files
committed
ANDROID: use common graphics
1 parent 0adddad commit 7781588

File tree

7 files changed

+80
-107
lines changed

7 files changed

+80
-107
lines changed

src/platform/android/jni/display.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern common::Graphics *graphics;
2222
//
2323
// Canvas implementation
2424
//
25-
Canvas::Canvas() : common::Canvas(),
25+
Canvas::Canvas() :
2626
_canvas(NULL),
2727
_clip(NULL) {
2828
}
@@ -49,15 +49,6 @@ bool Canvas::create(int w, int h) {
4949
return result;
5050
}
5151

52-
void Canvas::freeClip() {
53-
delete _clip;
54-
_clip = NULL;
55-
}
56-
57-
pixel_t *Canvas::getLine(int y) {
58-
return _canvas + (y * _w);
59-
}
60-
6152
void Canvas::setClip(int x, int y, int w, int h) {
6253
delete _clip;
6354
if (x != 0 || y != 0 || _w != w || _h != h) {
@@ -115,8 +106,8 @@ void Graphics::redraw() {
115106
trace("Unable to lock window buffer");
116107
} else {
117108
void *pixels = buffer.bits;
118-
int width = min(_w, min(buffer.width, _screen->width()));
119-
int height = min(_h, min(buffer.height, _screen->height()));
109+
int width = min(_w, min(buffer.width, _screen->_w));
110+
int height = min(_h, min(buffer.height, _screen->_h));
120111
for (int y = 0; y < height; y++) {
121112
pixel_t *line = _screen->getLine(y);
122113
memcpy((pixel_t *)pixels, line, width * sizeof(pixel_t));
@@ -165,11 +156,6 @@ void maUpdateScreen(void) {
165156
((Graphics *)graphics)->redraw();
166157
}
167158

168-
MAHandle maCreatePlaceholder(void) {
169-
MAHandle maHandle = (MAHandle) new Canvas();
170-
return maHandle;
171-
}
172-
173159
int maCreateDrawableImage(MAHandle maHandle, int width, int height) {
174160
int result = RES_OK;
175161
if (height > graphics->getHeight() * SIZE_LIMIT) {

src/platform/android/jni/display.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@
1818

1919
using namespace strlib;
2020

21-
struct Canvas : public common::Canvas {
22-
Canvas();
23-
virtual ~Canvas();
24-
25-
bool create(int w, int h);
26-
void freeClip();
27-
void setClip(int x, int y, int w, int h);
28-
pixel_t *getLine(int y);
29-
int x() { return _clip ? _clip->left : 0; }
30-
int y() { return _clip ? _clip->top : 0; }
31-
int w() { return _clip ? _clip->right : _w; }
32-
int h() { return _clip ? _clip->bottom : _h; }
33-
int width() { return _w; }
34-
int height() { return _h; }
35-
36-
int _w;
37-
int _h;
38-
pixel_t *_canvas;
39-
ARect *_clip;
40-
};
41-
4221
struct Graphics : common::Graphics {
4322
Graphics(android_app *app);
4423
virtual ~Graphics();

src/platform/common/canvas.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file is part of SmallBASIC
2+
//
3+
// Copyright(C) 2001-2014 Chris Warren-Smith.
4+
//
5+
// This program is distributed under the terms of the GPL v2.0 or later
6+
// Download the GNU Public License (GPL) from www.gnu.org
7+
//
8+
9+
#ifndef COMMON_CANVAS
10+
#define COMMON_CANVAS
11+
12+
typedef uint16_t pixel_t;
13+
14+
#if defined(_SDL)
15+
16+
#include <SDL.h>
17+
struct Canvas {
18+
Canvas();
19+
virtual ~Canvas();
20+
21+
bool create(int w, int h);
22+
void setClip(int x, int y, int w, int h);
23+
pixel_t *getLine(int y);
24+
int x() { return _clip ? _clip->x : 0; }
25+
int y() { return _clip ? _clip->y : 0; }
26+
int w() { return _clip ? _clip->w : _w; }
27+
int h() { return _clip ? _clip->h : _h; }
28+
29+
int _w;
30+
int _h;
31+
SDL_Surface *_canvas;
32+
SDL_Rect *_clip;
33+
};
34+
35+
#else
36+
37+
#include <android/rect.h>
38+
struct Canvas {
39+
Canvas();
40+
virtual ~Canvas();
41+
42+
bool create(int w, int h);
43+
void setClip(int x, int y, int w, int h);
44+
pixel_t *getLine(int y) { return _canvas + (y * _w); }
45+
int x() { return _clip ? _clip->left : 0; }
46+
int y() { return _clip ? _clip->top : 0; }
47+
int w() { return _clip ? _clip->right : _w; }
48+
int h() { return _clip ? _clip->bottom : _h; }
49+
50+
int _w;
51+
int _h;
52+
pixel_t *_canvas;
53+
ARect *_clip;
54+
};
55+
56+
#endif
57+
#endif
58+

src/platform/common/graphics.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#include "config.h"
10+
1011
#include "platform/common/graphics.h"
1112
#include "platform/common/utils.h"
1213
#include "common/device.h"
@@ -71,15 +72,6 @@ Font::~Font() {
7172
}
7273
}
7374

74-
//
75-
// Canvas implementation
76-
//
77-
Canvas::Canvas() {
78-
}
79-
80-
Canvas::~Canvas() {
81-
}
82-
8375
//
8476
// Graphics implementation
8577
//
@@ -121,11 +113,10 @@ void Graphics::drawImageRegion(Canvas *src, const MAPoint2d *dstPoint, const MAR
121113
if (_drawTarget && _drawTarget != src) {
122114
int destY = dstPoint->y;
123115
int srcH = srcRect->height;
124-
if (srcRect->top + srcRect->height > src->height()) {
125-
srcH = src->height() - srcRect->top;
116+
if (srcRect->top + srcRect->height > src->_h) {
117+
srcH = src->_h - srcRect->top;
126118
}
127-
int targetH = _drawTarget->height();
128-
for (int y = 0; y < srcH && destY < targetH; y++, destY++) {
119+
for (int y = 0; y < srcH && destY < _drawTarget->_h; y++, destY++) {
129120
pixel_t *line = src->getLine(y + srcRect->top) + srcRect->left;
130121
pixel_t *dstLine = _drawTarget->getLine(destY) + dstPoint->x;
131122
memcpy(dstLine, line, srcRect->width * sizeof(pixel_t));
@@ -257,8 +248,8 @@ int Graphics::getPixel(int posX, int posY) {
257248
if (_drawTarget
258249
&& posX > -1
259250
&& posY > -1
260-
&& posX < _drawTarget->width()
261-
&& posY < _drawTarget->height() - 1) {
251+
&& posX < _drawTarget->_w
252+
&& posY < _drawTarget->_h - 1) {
262253
pixel_t *line = _drawTarget->getLine(posY);
263254
result = line[posX];
264255
}
@@ -291,13 +282,19 @@ MAHandle Graphics::setDrawTarget(MAHandle maHandle) {
291282
} else {
292283
_drawTarget = (Canvas *)maHandle;
293284
}
294-
_drawTarget->freeClip();
285+
delete _drawTarget->_clip;
286+
_drawTarget->_clip = NULL;
295287
return (MAHandle) _drawTarget;
296288
}
297289

298290
//
299291
// maapi implementation
300292
//
293+
MAHandle maCreatePlaceholder(void) {
294+
MAHandle maHandle = (MAHandle) new Canvas();
295+
return maHandle;
296+
}
297+
301298
int maFontDelete(MAHandle maHandle) {
302299
if (maHandle != -1) {
303300
graphics->deleteFont((Font *)maHandle);

src/platform/common/graphics.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "platform/common/maapi.h"
1313
#include "platform/common/StringLib.h"
14+
#include "platform/common/canvas.h"
1415

1516
#include <ft2build.h>
1617
#include FT_FREETYPE_H
@@ -20,8 +21,6 @@
2021

2122
using namespace strlib;
2223

23-
typedef uint16_t pixel_t;
24-
2524
namespace common {
2625

2726
struct Glyph {
@@ -38,22 +37,6 @@ struct Font {
3837
Glyph _glyph[MAX_GLYPHS];
3938
};
4039

41-
struct Canvas {
42-
Canvas();
43-
virtual ~Canvas();
44-
45-
virtual bool create(int w, int h) = 0;
46-
virtual pixel_t *getLine(int y) = 0;
47-
virtual void setClip(int x, int y, int w, int h) = 0;
48-
virtual void freeClip() = 0;
49-
virtual int x() = 0;
50-
virtual int y() = 0;
51-
virtual int w() = 0;
52-
virtual int h() = 0;
53-
virtual int width() = 0;
54-
virtual int height() = 0;
55-
};
56-
5740
struct Graphics {
5841
Graphics();
5942
virtual ~Graphics();

src/platform/sdl/display.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern common::Graphics *graphics;
2020
//
2121
// Canvas implementation
2222
//
23-
Canvas::Canvas() : common::Canvas(),
23+
Canvas::Canvas() :
2424
_canvas(NULL),
2525
_clip(NULL) {
2626
}
@@ -29,8 +29,9 @@ Canvas::~Canvas() {
2929
if (_canvas != NULL) {
3030
SDL_FreeSurface(_canvas);
3131
}
32+
delete _clip;
3233
_canvas = NULL;
33-
freeClip();
34+
_clip = NULL;
3435
}
3536

3637
bool Canvas::create(int w, int h) {
@@ -45,11 +46,6 @@ bool Canvas::create(int w, int h) {
4546
return _canvas != NULL;
4647
}
4748

48-
void Canvas::freeClip() {
49-
delete _clip;
50-
_clip = NULL;
51-
}
52-
5349
pixel_t *Canvas::getLine(int y) {
5450
pixel_t *pixels = (pixel_t *)_canvas->pixels;
5551
return pixels + (y * _w);
@@ -101,8 +97,8 @@ void Graphics::redraw() {
10197
SDL_Rect srcrect;
10298
srcrect.x = 0;
10399
srcrect.y = 0;
104-
srcrect.w = _screen->width();
105-
srcrect.h = _screen->height();
100+
srcrect.w = _screen->_w;
101+
srcrect.h = _screen->_h;
106102

107103
SDL_Rect dstrect;
108104
dstrect.x = 0;
@@ -140,11 +136,6 @@ void maUpdateScreen(void) {
140136
((::Graphics *)graphics)->redraw();
141137
}
142138

143-
MAHandle maCreatePlaceholder(void) {
144-
MAHandle maHandle = (MAHandle) new ::Canvas();
145-
return maHandle;
146-
}
147-
148139
int maCreateDrawableImage(MAHandle maHandle, int width, int height) {
149140
int result = RES_OK;
150141
if (height > graphics->getHeight() * SIZE_LIMIT) {

src/platform/sdl/display.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,6 @@
1515
#include "platform/common/StringLib.h"
1616
#include "platform/common/graphics.h"
1717

18-
struct Canvas : public common::Canvas {
19-
Canvas();
20-
virtual ~Canvas();
21-
22-
bool create(int w, int h);
23-
void freeClip();
24-
void setClip(int x, int y, int w, int h);
25-
pixel_t *getLine(int y);
26-
int x() { return _clip ? _clip->x : 0; }
27-
int y() { return _clip ? _clip->y : 0; }
28-
int w() { return _clip ? _clip->w : _w; }
29-
int h() { return _clip ? _clip->h : _h; }
30-
int width() { return _w; }
31-
int height() { return _h; }
32-
33-
int _w;
34-
int _h;
35-
SDL_Surface *_canvas;
36-
SDL_Rect *_clip;
37-
};
38-
3918
struct Graphics : common::Graphics {
4019
Graphics(SDL_Window *window);
4120
virtual ~Graphics();

0 commit comments

Comments
 (0)