1111#include " ui/utils.h"
1212#include " platform/sdl/display.h"
1313
14- extern common ::Graphics *graphics;
14+ extern ui ::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
16+ #define PIXELFORMAT SDL_PIXELFORMAT_RGB888
2117
2218//
2319// Canvas implementation
2420//
2521Canvas::Canvas () :
22+ _w(0 ),
23+ _h(0 ),
24+ _ownerSurface(false ),
25+ _pixels(NULL ),
2626 _surface(NULL ),
2727 _clip(NULL ) {
2828}
2929
3030Canvas::~Canvas () {
31- if (_surface != NULL ) {
31+ if (_surface != NULL && _ownerSurface ) {
3232 SDL_FreeSurface (_surface);
3333 }
3434 delete _clip;
@@ -42,12 +42,14 @@ bool Canvas::create(int w, int h) {
4242 _h = h;
4343 int bpp;
4444 Uint32 rmask, gmask, bmask, amask;
45- SDL_PixelFormatEnumToMasks (PIXEL_FORMAT, &bpp, &rmask, &gmask, &bmask, &amask);
45+ SDL_PixelFormatEnumToMasks (PIXELFORMAT, &bpp, &rmask, &gmask, &bmask, &amask);
46+ _ownerSurface = true ;
4647 _surface = SDL_CreateRGBSurface (0 , w, h, bpp, rmask, gmask, bmask, amask);
48+ _pixels = (pixel_t *)_surface->pixels ;
4749 return _surface != NULL ;
4850}
4951
50- void Canvas::copy (Canvas *src, const MARect *srcRect, int destX, int destY) {
52+ void Canvas::drawRegion (Canvas *src, const MARect *srcRect, int destX, int destY) {
5153 SDL_Rect srcrect;
5254 srcrect.x = srcRect->left ;
5355 srcrect.y = srcRect->top ;
@@ -63,9 +65,13 @@ void Canvas::copy(Canvas *src, const MARect *srcRect, int destX, int destY) {
6365 SDL_BlitSurface (src->_surface , &srcrect, _surface, &dstrect);
6466}
6567
66- pixel_t *Canvas::getLine (int y) {
67- pixel_t *pixels = (pixel_t *)_surface->pixels ;
68- return pixels + (y * _w);
68+ void Canvas::fillRect (int x, int y, int w, int h, pixel_t color) {
69+ SDL_Rect rect;
70+ rect.x = x;
71+ rect.y = y;
72+ rect.w = w;
73+ rect.h = h;
74+ SDL_FillRect (_surface, &rect, color);
6975}
7076
7177void Canvas::setClip (int x, int y, int w, int h) {
@@ -81,60 +87,78 @@ void Canvas::setClip(int x, int y, int w, int h) {
8187 }
8288}
8389
90+ void Canvas::setSurface (SDL_Surface *surface, int w, int h) {
91+ _surface = surface;
92+ _pixels = (pixel_t *)_surface->pixels ;
93+ _ownerSurface = false ;
94+ _w = w;
95+ _h = h;
96+ }
97+
8498//
8599// Graphics implementation
86100//
87- Graphics::Graphics (SDL_Window *window) : common::Graphics(),
88- _renderer(NULL ),
89- _texture(NULL ),
90- _window(window) {
101+ Graphics::Graphics (SDL_Window *window) : ui::Graphics(),
102+ _window(window),
103+ _surface(NULL ) {
91104}
92105
93106Graphics::~Graphics () {
94107 logEntered ();
95- SDL_DestroyTexture (_texture);
96- SDL_DestroyRenderer (_renderer);
97108}
98109
99110bool Graphics::construct (const char *font, const char *boldFont) {
100111 logEntered ();
101112
113+ int w, h;
102114 bool result = false ;
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);
115+ SDL_GetWindowSize (_window, &w, &h);
116+
117+ SDL_Surface *surface = SDL_GetWindowSurface (_window);
118+ if (surface->format ->format != PIXELFORMAT) {
119+ deviceLog (" Unexpected window surface format %d" , surface->format ->format );
120+ _surface = surface;
108121 }
109- if (_texture != NULL && loadFonts (font, boldFont)) {
122+
123+ if (loadFonts (font, boldFont)) {
110124 _screen = new Canvas ();
111- if (_screen && _screen->create (getWidth (), getHeight ())) {
125+ result = _screen != NULL ;
126+ if (result) {
127+ if (_surface == NULL ) {
128+ _screen->setSurface (SDL_GetWindowSurface (_window), w, h);
129+ } else {
130+ result = _screen->create (w, h);
131+ }
132+ }
133+ if (result) {
112134 _drawTarget = _screen;
113135 maSetColor (DEFAULT_BACKGROUND);
114- result = true ;
115136 }
116137 }
117138 return result;
118139}
119140
120141void Graphics::redraw () {
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);
142+ if (_surface != NULL ) {
143+ SDL_Surface *src = ((Canvas *)_screen)->_surface ;
144+ SDL_BlitSurface (src, NULL , _surface, NULL );
145+ }
146+ SDL_UpdateWindowSurface (_window);
125147}
126148
127- void Graphics::resize () {
149+ void Graphics::resize (int w, int h ) {
128150 logEntered ();
129- bool drawScreen = (_drawTarget == _screen);
130- delete _screen;
131- _screen = new ::Canvas ();
132- _screen->create (getWidth (), getHeight ());
133- _drawTarget = drawScreen ? _screen : NULL ;
134-
135- SDL_DestroyTexture (_texture);
136- _texture = SDL_CreateTexture (_renderer, PIXEL_FORMAT,
137- SDL_TEXTUREACCESS_STREAMING, _w, _h);
151+ SDL_Surface *surface = SDL_GetWindowSurface (_window);
152+ if (_surface == NULL ) {
153+ _screen->setSurface (surface, w, h);
154+ } else {
155+ bool drawScreen = (_drawTarget == _screen);
156+ delete _screen;
157+ _screen = new ::Canvas ();
158+ _screen->create (w, h);
159+ _drawTarget = drawScreen ? _screen : NULL ;
160+ _surface = surface;
161+ }
138162}
139163
140164bool Graphics::loadFonts (const char *font, const char *boldFont) {
0 commit comments