6 static int gfx_newcol(int r, int g, int b);
7 static void gfx_color(int c);
8 static void gfx_fillrect(struct wt_rect *r);
9 static void gfx_line(int x0, int y0, int x1, int y1);
11 static struct wt_graphics gfx = {
13 gfx_newcol, gfx_color,
18 static SDL_Surface *fbsurf;
19 static SDL_PixelFormat *pfmt;
20 static int fb_width = 800, fb_height = 600;
29 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
30 fprintf(stderr, "failed to initialize SDL\n");
34 if(!(fbsurf = SDL_SetVideoMode(fb_width, fb_height, 32, SDL_SWSURFACE | SDL_RESIZABLE))) {
35 fprintf(stderr, "failed to set video mode\n");
39 pfmt = fbsurf->format;
41 SDL_FillRect(fbsurf, 0, 0);
43 if(wt_init(fb_width, fb_height, &gfx) == -1) {
46 wt_window(0, "foo", WT_WS_DEFAULT, 100, 100, 200, 200);
48 while(SDL_WaitEvent(&ev)) {
55 if(ev.key.keysym.sym == SDLK_ESCAPE) {
58 wt_inp_key(ev.key.keysym.sym, ev.key.state == SDL_PRESSED);
61 case SDL_MOUSEBUTTONDOWN:
62 case SDL_MOUSEBUTTONUP:
63 wt_inp_mouse(ev.button.button - SDL_BUTTON_LEFT, ev.button.state == SDL_PRESSED,
64 ev.button.x, ev.button.y);
68 wt_inp_motion(ev.motion.x, ev.motion.y);
72 fb_width = ev.resize.w;
73 fb_height = ev.resize.h;
74 wt_viewport(0, 0, ev.resize.w, ev.resize.h);
81 if(SDL_MUSTLOCK(fbsurf)) {
82 SDL_LockSurface(fbsurf);
86 if(SDL_MUSTLOCK(fbsurf)) {
87 SDL_UnlockSurface(fbsurf);
90 num_upd = wt_num_upd();
91 for(i=0; i<num_upd; i++) {
92 rect = wt_upd_rect(i);
93 SDL_UpdateRect(fbsurf, rect->x, rect->y, rect->w, rect->h);
103 /* ------ graphics callbacks -------- */
104 static uint32_t colors[32];
105 static int maxcol, curcol;
107 static int gfx_newcol(int r, int g, int b)
112 pcol = ((r >> pfmt->Rloss) << pfmt->Rshift) & pfmt->Rmask;
113 pcol |= ((g >> pfmt->Gloss) << pfmt->Gshift) & pfmt->Gmask;
114 pcol |= ((b >> pfmt->Bloss) << pfmt->Bshift) & pfmt->Bmask;
116 for(i=0; i<maxcol; i++) {
117 if(colors[i] == pcol) {
122 if(maxcol >= 32) return -1;
124 colors[maxcol] = pcol;
128 static void gfx_color(int c)
133 static void gfx_fillrect(struct wt_rect *r)
135 int i, j, x, y, w, h;
136 uint32_t pcol = colors[curcol];
152 if(x + w >= fb_width) {
155 if(y + h >= fb_height) {
159 fbptr = fb + y * (fbsurf->pitch >> 2) + x;
164 fbptr += fbsurf->pitch >> 2;
168 static void gfx_line(int x0, int y0, int x1, int y1)