diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-06 06:38:28 -0500 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-06 06:38:28 -0500 |
commit | a096d1b1ffaa22585322c3e2619e88c0030de566 (patch) | |
tree | 58687160d7325520c6098a510f0fa19d886cb0f7 /libwinnie/src/sdl | |
parent | af75fce2fb87fb0e285b1c876fb14bccb3c0def7 (diff) |
clang-format for readability
Diffstat (limited to 'libwinnie/src/sdl')
-rw-r--r-- | libwinnie/src/sdl/event.cc | 130 | ||||
-rw-r--r-- | libwinnie/src/sdl/event.h | 19 | ||||
-rw-r--r-- | libwinnie/src/sdl/gfx.cc | 196 | ||||
-rw-r--r-- | libwinnie/src/sdl/gfx.h | 31 | ||||
-rw-r--r-- | libwinnie/src/sdl/keyboard.cc | 36 | ||||
-rw-r--r-- | libwinnie/src/sdl/keyboard.h | 2 | ||||
-rw-r--r-- | libwinnie/src/sdl/mouse.cc | 145 | ||||
-rw-r--r-- | libwinnie/src/sdl/mouse.h | 2 |
8 files changed, 304 insertions, 257 deletions
diff --git a/libwinnie/src/sdl/event.cc b/libwinnie/src/sdl/event.cc index 5835704..8e5c234 100644 --- a/libwinnie/src/sdl/event.cc +++ b/libwinnie/src/sdl/event.cc @@ -20,90 +20,90 @@ Author: Eleni Maria Stea <elene.mst@gmail.com> */ #ifdef WINNIE_SDL -#include <stdlib.h> #include <SDL/SDL.h> +#include <stdlib.h> #include "sdl/event.h" #include "sdl/keyboard.h" #include "sdl/mouse.h" #include "wm.h" -enum { - TIMER_EVENT = SDL_USEREVENT -}; +enum { TIMER_EVENT = SDL_USEREVENT }; SDL_Event sdl_event; -void process_events() +void +process_events() { - wm->process_windows(); - if(!SDL_WaitEvent(&sdl_event)) { - return; - } - - do { - switch(sdl_event.type) { - case SDL_KEYDOWN: - case SDL_KEYUP: - process_keyboard_event(); - break; - case SDL_MOUSEMOTION: - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: - process_mouse_event(); - break; - case SDL_QUIT: - exit(0); - - case TIMER_EVENT: - { - Window *win = (Window*)sdl_event.user.data1; - TimerFuncType func = win->get_timer_callback(); - if(func) { - func(win); - } else { - fprintf(stderr, "timer gone off but window has no timer callback!\n"); - } - } - break; - - default: - break; - } - } while(SDL_PollEvent(&sdl_event)); + wm->process_windows(); + if (!SDL_WaitEvent(&sdl_event)) { + return; + } + + do { + switch (sdl_event.type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + process_keyboard_event(); + break; + case SDL_MOUSEMOTION: + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + process_mouse_event(); + break; + case SDL_QUIT: + exit(0); + + case TIMER_EVENT: { + Window *win = (Window *) sdl_event.user.data1; + TimerFuncType func = win->get_timer_callback(); + if (func) { + func(win); + } else { + fprintf(stderr, "timer gone off but window has no timer callback!\n"); + } + } break; + + default: + break; + } + } while (SDL_PollEvent(&sdl_event)); } struct TimerData { - SDL_TimerID sdl_timer; - Window *win; - TimerMode mode; + SDL_TimerID sdl_timer; + Window *win; + TimerMode mode; }; -static unsigned int timer_callback(unsigned int interval, void *cls) +static unsigned int +timer_callback(unsigned int interval, void *cls) { - TimerData *td = (TimerData*)cls; - - SDL_Event ev; - ev.type = TIMER_EVENT; - ev.user.data1 = td->win; - SDL_PushEvent(&ev); - - if(td->mode == TIMER_ONESHOT) { - delete td; - return 0; - } - return interval; // repeat at same interval + TimerData *td = (TimerData *) cls; + + SDL_Event ev; + ev.type = TIMER_EVENT; + ev.user.data1 = td->win; + SDL_PushEvent(&ev); + + if (td->mode == TIMER_ONESHOT) { + delete td; + return 0; + } + return interval; // repeat at same interval } -void set_window_timer(Window *win, unsigned int msec, TimerMode mode) +void +set_window_timer(Window *win, unsigned int msec, TimerMode mode) { - if(!win->get_timer_callback()) { - fprintf(stderr, "trying to start a timer without having a timer callback!\n"); - return; - } - TimerData *td = new TimerData; - td->win = win; - td->mode = mode; - td->sdl_timer = SDL_AddTimer(msec, timer_callback, td); + if (!win->get_timer_callback()) { + fprintf(stderr, + "trying to start a timer without having a timer callback!\n"); + return; + } + TimerData *td = new TimerData; + td->win = win; + td->mode = mode; + td->sdl_timer = SDL_AddTimer(msec, timer_callback, td); } #endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/event.h b/libwinnie/src/sdl/event.h index 5c1db45..036dea5 100644 --- a/libwinnie/src/sdl/event.h +++ b/libwinnie/src/sdl/event.h @@ -26,22 +26,25 @@ class Window; typedef void (*DisplayFuncType)(Window *win); typedef void (*KeyboardFuncType)(Window *win, int key, bool pressed); -typedef void (*MouseButtonFuncType)(Window *win, int bn, bool pressed, int x, int y); +typedef void (*MouseButtonFuncType)( + Window *win, int bn, bool pressed, int x, int y); typedef void (*MouseMotionFuncType)(Window *win, int x, int y); typedef void (*TimerFuncType)(Window *win); struct Callbacks { - DisplayFuncType display; - KeyboardFuncType keyboard; - MouseButtonFuncType button; - MouseMotionFuncType motion; - TimerFuncType timer; + DisplayFuncType display; + KeyboardFuncType keyboard; + MouseButtonFuncType button; + MouseMotionFuncType motion; + TimerFuncType timer; }; void process_events(); -enum TimerMode {TIMER_ONESHOT, TIMER_REPEAT}; +enum TimerMode { TIMER_ONESHOT, TIMER_REPEAT }; -void set_window_timer(Window *win, unsigned int msec, TimerMode mode = TIMER_ONESHOT); +void set_window_timer(Window *win, + unsigned int msec, + TimerMode mode = TIMER_ONESHOT); #endif diff --git a/libwinnie/src/sdl/gfx.cc b/libwinnie/src/sdl/gfx.cc index a955358..bb5eb68 100644 --- a/libwinnie/src/sdl/gfx.cc +++ b/libwinnie/src/sdl/gfx.cc @@ -20,10 +20,10 @@ Author: Eleni Maria Stea <elene.mst@gmail.com> */ #ifdef WINNIE_SDL -#include <stdio.h> -#include <stdlib.h> #include <SDL/SDL.h> #include <stdint.h> +#include <stdio.h> +#include <stdlib.h> #include "sdl/gfx.h" #include "winnie.h" @@ -31,132 +31,148 @@ Author: Eleni Maria Stea <elene.mst@gmail.com> static SDL_Surface *fbsurf; struct Graphics { - Rect screen_rect; - Rect clipping_rect; - int color_depth; // bits per pixel - Pixmap *pixmap; + Rect screen_rect; + Rect clipping_rect; + int color_depth; // bits per pixel + Pixmap *pixmap; }; static Graphics *gfx; -bool init_gfx() +bool +init_gfx() { - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { - fprintf(stderr, "failed to initialize SDL\n"); - return false; - } - - if(!(gfx = (Graphics*)malloc(sizeof *gfx))) { - return false; - } - - get_subsys()->graphics_offset = (intptr_t)(gfx); - - Rect scr_rect(0, 0, 1280, 853); - gfx->screen_rect = scr_rect; - gfx->color_depth = 32; - - if(!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) { - fprintf(stderr, "Failed to set video mode\n"); - return false; - } - SDL_ShowCursor(0); - - if(!(gfx->pixmap = (Pixmap*)malloc(sizeof(Pixmap)))) { - fprintf(stderr, "Failed to allocate pixmap.\n"); - return false; - } - - gfx->pixmap->width = gfx->screen_rect.width; - gfx->pixmap->height = gfx->screen_rect.height; - - int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8; - if(!(gfx->pixmap->pixels = (unsigned char*)malloc(fbsize))) { - fprintf(stderr, "failed to allocate the pixmap framebuffer.\n"); - return false; - } - - set_clipping_rect(gfx->screen_rect); - - return true; + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { + fprintf(stderr, "failed to initialize SDL\n"); + return false; + } + + if (!(gfx = (Graphics *) malloc(sizeof *gfx))) { + return false; + } + + get_subsys()->graphics_offset = (intptr_t) (gfx); + + Rect scr_rect(0, 0, 1280, 853); + gfx->screen_rect = scr_rect; + gfx->color_depth = 32; + + if (!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, + gfx->screen_rect.height, + gfx->color_depth, + 0))) { + fprintf(stderr, "Failed to set video mode\n"); + return false; + } + SDL_ShowCursor(0); + + if (!(gfx->pixmap = (Pixmap *) malloc(sizeof(Pixmap)))) { + fprintf(stderr, "Failed to allocate pixmap.\n"); + return false; + } + + gfx->pixmap->width = gfx->screen_rect.width; + gfx->pixmap->height = gfx->screen_rect.height; + + int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8; + if (!(gfx->pixmap->pixels = (unsigned char *) malloc(fbsize))) { + fprintf(stderr, "failed to allocate the pixmap framebuffer.\n"); + return false; + } + + set_clipping_rect(gfx->screen_rect); + + return true; } -void destroy_gfx() +void +destroy_gfx() { - free(gfx->pixmap->pixels); - gfx->pixmap->pixels = 0; - free(gfx->pixmap); - free(gfx); - SDL_Quit(); + free(gfx->pixmap->pixels); + gfx->pixmap->pixels = 0; + free(gfx->pixmap); + free(gfx); + SDL_Quit(); } -unsigned char *get_framebuffer() +unsigned char * +get_framebuffer() { - return gfx->pixmap->pixels; + return gfx->pixmap->pixels; } -Pixmap *get_framebuffer_pixmap() +Pixmap * +get_framebuffer_pixmap() { - return gfx->pixmap; + return gfx->pixmap; } -Rect get_screen_size() +Rect +get_screen_size() { - return gfx->screen_rect; + return gfx->screen_rect; } -int get_color_depth() +int +get_color_depth() { - return gfx->color_depth; + return gfx->color_depth; } -void set_clipping_rect(const Rect &rect) +void +set_clipping_rect(const Rect &rect) { - gfx->clipping_rect = rect_intersection(rect, get_screen_size()); + gfx->clipping_rect = rect_intersection(rect, get_screen_size()); } -const Rect &get_clipping_rect() +const Rect & +get_clipping_rect() { - return gfx->clipping_rect; + return gfx->clipping_rect; } - -void set_cursor_visibility(bool visible) +void +set_cursor_visibility(bool visible) { } -void gfx_update(const Rect &upd_rect) +void +gfx_update(const Rect &upd_rect) { - if(SDL_MUSTLOCK(fbsurf)) { - SDL_LockSurface(fbsurf); - } - - Rect rect = rect_intersection(upd_rect, gfx->screen_rect); - - unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; - unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; - - for(int i=0; i<rect.height; i++) { - memcpy(dptr, sptr, rect.width * 4); - sptr += gfx->screen_rect.width * 4; - dptr += gfx->screen_rect.width * 4; - } - - if(SDL_MUSTLOCK(fbsurf)) { - SDL_UnlockSurface(fbsurf); - } - SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height); + if (SDL_MUSTLOCK(fbsurf)) { + SDL_LockSurface(fbsurf); + } + + Rect rect = rect_intersection(upd_rect, gfx->screen_rect); + + unsigned char *sptr + = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; + unsigned char *dptr = (unsigned char *) fbsurf->pixels + + (rect.y * gfx->screen_rect.width + rect.x) * 4; + + for (int i = 0; i < rect.height; i++) { + memcpy(dptr, sptr, rect.width * 4); + sptr += gfx->screen_rect.width * 4; + dptr += gfx->screen_rect.width * 4; + } + + if (SDL_MUSTLOCK(fbsurf)) { + SDL_UnlockSurface(fbsurf); + } + SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height); } -void wait_vsync() +void +wait_vsync() { } -void get_rgb_order(int *r, int *g, int *b) +void +get_rgb_order(int *r, int *g, int *b) { - *r = fbsurf->format->Rshift / 8; - *g = fbsurf->format->Gshift / 8; - *b = fbsurf->format->Bshift / 8; + *r = fbsurf->format->Rshift / 8; + *g = fbsurf->format->Gshift / 8; + *b = fbsurf->format->Bshift / 8; } #endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/gfx.h b/libwinnie/src/sdl/gfx.h index c14c22a..3674d86 100644 --- a/libwinnie/src/sdl/gfx.h +++ b/libwinnie/src/sdl/gfx.h @@ -42,14 +42,27 @@ void fill_rect(const Rect &rect, int r, int g, int b); void set_cursor_visibility(bool visible); -void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, - const Rect &dest_rect, int dest_x, int dest_y); - -void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, - const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b); - -void draw_line(Pixmap *pixmap, int x0, int y0, int x1, int y1, int r, int g, int b); -void draw_polygon(Pixmap *pixmap, int *vpos, int *vtex, int num_verts, int r, int g, int b); +void blit(unsigned char *src_img, + const Rect &src_rect, + unsigned char *dest_img, + const Rect &dest_rect, + int dest_x, + int dest_y); + +void blit_key(unsigned char *src_img, + const Rect &src_rect, + unsigned char *dest_img, + const Rect &dest_rect, + int dest_x, + int dest_y, + int key_r, + int key_g, + int key_b); + +void +draw_line(Pixmap *pixmap, int x0, int y0, int x1, int y1, int r, int g, int b); +void draw_polygon( + Pixmap *pixmap, int *vpos, int *vtex, int num_verts, int r, int g, int b); void gfx_update(const Rect &rect); @@ -57,4 +70,4 @@ void wait_vsync(); // vertical synchronization void get_rgb_order(int *r, int *g, int *b); -#endif //GFX_H_ +#endif // GFX_H_ diff --git a/libwinnie/src/sdl/keyboard.cc b/libwinnie/src/sdl/keyboard.cc index 93f2e20..60edc8d 100644 --- a/libwinnie/src/sdl/keyboard.cc +++ b/libwinnie/src/sdl/keyboard.cc @@ -28,31 +28,35 @@ Author: Eleni Maria Stea <elene.mst@gmail.com> extern SDL_Event sdl_event; -bool init_keyboard() +bool +init_keyboard() { - return true; + return true; } -void destroy_keyboard() +void +destroy_keyboard() { } -int get_keyboard_fd() +int +get_keyboard_fd() { - return -1; + return -1; } -void process_keyboard_event() +void +process_keyboard_event() { - int key = sdl_event.key.keysym.sym; - - Window *focused_win = wm->get_focused_window(); - if(focused_win) { - KeyboardFuncType keyb_callback = focused_win->get_keyboard_callback(); - if(keyb_callback) { - bool pressed = sdl_event.key.state == SDL_PRESSED; - keyb_callback(focused_win, key, pressed); - } - } + int key = sdl_event.key.keysym.sym; + + Window *focused_win = wm->get_focused_window(); + if (focused_win) { + KeyboardFuncType keyb_callback = focused_win->get_keyboard_callback(); + if (keyb_callback) { + bool pressed = sdl_event.key.state == SDL_PRESSED; + keyb_callback(focused_win, key, pressed); + } + } } #endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/keyboard.h b/libwinnie/src/sdl/keyboard.h index 548ddbb..1366057 100644 --- a/libwinnie/src/sdl/keyboard.h +++ b/libwinnie/src/sdl/keyboard.h @@ -28,4 +28,4 @@ void destroy_keyboard(); int get_keyboard_fd(); void process_keyboard_event(); -#endif // KEYBOARD_H_ +#endif // KEYBOARD_H_ diff --git a/libwinnie/src/sdl/mouse.cc b/libwinnie/src/sdl/mouse.cc index b5eac68..9ec6605 100644 --- a/libwinnie/src/sdl/mouse.cc +++ b/libwinnie/src/sdl/mouse.cc @@ -22,108 +22,119 @@ Author: Eleni Maria Stea <elene.mst@gmail.com> #ifdef WINNIE_SDL #include <SDL/SDL.h> -#include <stdlib.h> #include <stdint.h> +#include <stdlib.h> #include "sdl/mouse.h" -#include "wm.h" #include "window.h" #include "winnie.h" +#include "wm.h" extern SDL_Event sdl_event; struct Mouse { - int pointer_x; - int pointer_y; - int bnstate; + int pointer_x; + int pointer_y; + int bnstate; }; static Mouse *mouse; -bool init_mouse() +bool +init_mouse() { - if(!(mouse = (Mouse*)malloc(sizeof *mouse))) { - return false; - } - get_subsys()->mouse_offset = (intptr_t)(mouse); + if (!(mouse = (Mouse *) malloc(sizeof *mouse))) { + return false; + } + get_subsys()->mouse_offset = (intptr_t) (mouse); - memset(mouse, 0, sizeof *mouse); - return true; + memset(mouse, 0, sizeof *mouse); + return true; } -void destroy_mouse() +void +destroy_mouse() { - free(mouse); + free(mouse); } -void set_mouse_bounds(const Rect &rect) +void +set_mouse_bounds(const Rect &rect) { } -int get_mouse_fd() +int +get_mouse_fd() { - return -1; + return -1; } -void process_mouse_event() +void +process_mouse_event() { - int bn; - MouseMotionFuncType motion_callback = 0; - MouseButtonFuncType button_callback = 0; - - Window *win; - if(!(win = wm->get_grab_window())) { - win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y); - if(win) { - wm->set_focused_window(win); - } - else { - wm->set_focused_window(0); - } - } - - switch(sdl_event.type) { - case SDL_MOUSEMOTION: - mouse->pointer_x = sdl_event.motion.x; - mouse->pointer_y = sdl_event.motion.y; - if(win && (motion_callback = win->get_mouse_motion_callback())) { - Rect rect = win->get_absolute_rect(); - motion_callback(win, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y); - } - break; - - case SDL_MOUSEBUTTONUP: - case SDL_MOUSEBUTTONDOWN: - bn = sdl_event.button.button - SDL_BUTTON_LEFT; - if(sdl_event.button.state == SDL_PRESSED) { - mouse->bnstate |= 1 << bn; - } - else { - mouse->bnstate &= ~(1 << bn); - } - if(win && (button_callback = win->get_mouse_button_callback())) { - Rect rect = win->get_absolute_rect(); - button_callback(win, bn, sdl_event.button.state, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y); - } - } + int bn; + MouseMotionFuncType motion_callback = 0; + MouseButtonFuncType button_callback = 0; + + Window *win; + if (!(win = wm->get_grab_window())) { + win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y); + if (win) { + wm->set_focused_window(win); + } else { + wm->set_focused_window(0); + } + } + + switch (sdl_event.type) { + case SDL_MOUSEMOTION: + mouse->pointer_x = sdl_event.motion.x; + mouse->pointer_y = sdl_event.motion.y; + if (win && (motion_callback = win->get_mouse_motion_callback())) { + Rect rect = win->get_absolute_rect(); + motion_callback( + win, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y); + } + break; + + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEBUTTONDOWN: + bn = sdl_event.button.button - SDL_BUTTON_LEFT; + if (sdl_event.button.state == SDL_PRESSED) { + mouse->bnstate |= 1 << bn; + } else { + mouse->bnstate &= ~(1 << bn); + } + if (win && (button_callback = win->get_mouse_button_callback())) { + Rect rect = win->get_absolute_rect(); + button_callback(win, + bn, + sdl_event.button.state, + mouse->pointer_x - rect.x, + mouse->pointer_y - rect.y); + } + } } -void get_pointer_pos(int *x, int *y) +void +get_pointer_pos(int *x, int *y) { - *x = mouse->pointer_x; - *y = mouse->pointer_y; + *x = mouse->pointer_x; + *y = mouse->pointer_y; } -int get_button_state() +int +get_button_state() { - return mouse->bnstate; + return mouse->bnstate; } -int get_button(int bn) +int +get_button(int bn) { - if(bn < 0 || bn >= 3) { - return 0; - } - return (mouse->bnstate & (1 << bn)) != 0; + if (bn < 0 || bn >= 3) { + return 0; + } + return (mouse->bnstate & (1 << bn)) != 0; } #endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/mouse.h b/libwinnie/src/sdl/mouse.h index 6fbe711..d47a674 100644 --- a/libwinnie/src/sdl/mouse.h +++ b/libwinnie/src/sdl/mouse.h @@ -36,4 +36,4 @@ void get_pointer_pos(int *x, int *y); int get_button_state(); int get_button(int bn); -#endif // MOUSE_H_ +#endif // MOUSE_H_ |