-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibsdl.c
64 lines (57 loc) · 1.61 KB
/
libsdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "libsdl.h"
void startSDL(){
if(SDL_Init(SDL_INIT_VIDEO) == -1){
fprintf(stderr, "Error: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
}
void endSDL(Window *w){
free(w);
SDL_Quit();
}
Window* createWindow(int width, int height, char *title){
startSDL();
Window *w = malloc(sizeof(*w));
w->width = width;
w->height = height;
w->closed = 0;
w->screen = malloc(sizeof(SDL_Surface));
w->screen = SDL_SetVideoMode(w->width, w->height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if(w->screen == NULL){
fprintf(stderr, "Error: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
setColor(w, 0xFFF);
SDL_WM_SetCaption(title, NULL);
SDL_FillRect(w->screen, NULL, w->color);
SDL_Flip(w->screen);
setColor(w, 0x000);
return w;
}
RGB hexToRGB(const int hex){
int m = (hex > 0xFFF) ? 0x1 : 0x11;
RGB col = {
(hex >> 0x8*(0x2-(m >> 0x4)) & 0xFF/m)*m,
(hex >> 0x4*(0x2-(m >> 0x4)) & 0xFF/m)*m,
(hex >> 0x0*(0x2-(m >> 0x4)) & 0xFF/m)*m
};
return col;
}
void setColor(Window *w, const int hex){
RGB col = hexToRGB(hex);
w->color = SDL_MapRGB(w->screen->format, col.red, col.green, col.blue);
}
void drawRect(Window *w, int x, int y, int width, int height){
SDL_Rect pos;
pos.x = x;
pos.y = y;
SDL_Surface *rect = SDL_CreateRGBSurface(SDL_HWSURFACE, width, height, 32, 0, 0, 0, 0);
SDL_FillRect(rect, NULL, w->color);
SDL_BlitSurface(rect, NULL, w->screen, &pos);
SDL_FreeSurface(rect);
}
void checkForClose(Window *w){
SDL_Event e = w->events;
if(e.type == SDL_QUIT || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE))
w->closed = 1;
}