1313
1414extern 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//
1925Canvas::Canvas () :
20- _canvas (NULL ),
26+ _surface (NULL ),
2127 _clip(NULL ) {
2228}
2329
2430Canvas::~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
7871void 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//
9487Graphics::Graphics (SDL_Window *window) : common::Graphics(),
88+ _renderer(NULL ),
89+ _texture(NULL ),
9590 _window(window) {
9691}
9792
9893Graphics::~Graphics () {
9994 logEntered ();
95+ SDL_DestroyTexture (_texture);
96+ SDL_DestroyRenderer (_renderer);
10097}
10198
10299bool 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
118120void 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
122127void 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
131140bool Graphics::loadFonts (const char *font, const char *boldFont) {
0 commit comments