added clipping rectangles
[winnie] / src / sdl / gfx.cc
1 #ifdef WINNIE_SDL
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <SDL/SDL.h>
5 #include "gfx.h"
6
7 static SDL_Surface *fbsurf;
8
9 static Rect screen_rect = {0, 0, 1024, 768};
10 static Rect clipping_rect;
11
12 static int color_depth = 32; // bits per pixel
13
14 static Pixmap *pixmap;
15
16 bool init_gfx()
17 {
18         if(SDL_Init(SDL_INIT_VIDEO) == -1) {
19                 fprintf(stderr, "failed to initialize SDL\n");
20                 return false;
21         }
22
23         if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) {
24                 fprintf(stderr, "failed to set video mode\n");
25                 return false;
26         }
27         SDL_ShowCursor(0);
28
29         pixmap = new Pixmap;
30         pixmap->width = screen_rect.width;
31         pixmap->height = screen_rect.height;
32         pixmap->pixels = (unsigned char*)fbsurf->pixels;
33
34         set_clipping_rect(screen_rect);
35
36         return true;
37 }
38
39 void destroy_gfx()
40 {
41         pixmap->pixels = 0;
42         delete pixmap;
43         SDL_Quit();
44 }
45
46 unsigned char *get_framebuffer()
47 {
48         return (unsigned char*)fbsurf->pixels;
49 }
50
51 Pixmap *get_framebuffer_pixmap()
52 {
53         return pixmap;
54 }
55
56 Rect get_screen_size()
57 {
58         return screen_rect;
59 }
60
61 int get_color_depth()
62 {
63         return color_depth;
64 }
65
66 /*void set_clipping_rect(const Rect &rect)
67 {
68         clipping_rect = rect_intersection(rect, screen_rect);
69
70         SDL_Rect sdl_rect;
71         sdl_rect.x = clipping_rect.x;
72         sdl_rect.y = clipping_rect.y;
73         sdl_rect.w = clipping_rect.width;
74         sdl_rect.h = clipping_rect.height;
75
76         SDL_SetClipRect(fbsurf, &sdl_rect);
77 }
78
79 const Rect &get_clipping_rect()
80 {
81         return clipping_rect;
82 }
83
84 void clear_screen(int r, int g, int b)
85 {
86         fill_rect(screen_rect, r, g, b);
87 }
88
89 void fill_rect(const Rect &rect, int r, int g, int b)
90 {
91         uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
92         
93         SDL_Rect sdl_rect;
94         sdl_rect.x = rect.x;
95         sdl_rect.y = rect.y;
96         sdl_rect.w = rect.width;
97         sdl_rect.h = rect.height;
98
99         SDL_FillRect(fbsurf, &sdl_rect, color);
100 }*/
101
102 void set_cursor_visibility(bool visible)
103 {
104 }
105
106 void gfx_update()
107 {
108         SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
109 }
110
111 void wait_vsync()
112 {
113 }
114
115 #endif // WINNIE_SDL