-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
81 lines (63 loc) · 1.59 KB
/
graphics.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "graphics.h"
graphics::graphics(const char* caption, int res_x, int res_y, int bpp)
{
// Initialize SDL
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(res_x, res_y, bpp, SDL_HWSURFACE);
SDL_WM_SetCaption(caption, NULL);
// Set screen clearing colour
clear_colour = SDL_MapRGB(screen->format, 0, 0, 0);
list_len = 0;
list_head = NULL;
}
int graphics::add_object(graphics_obj *obj)
{
obj_list *list = new obj_list;
list->obj = obj;
list->next = NULL;
// First
if (list_head == NULL) {
list_head = list;
} else {
// Append to list
obj_list *list_item = list_head;
while (list_item->next != NULL) {
list_item = list_item->next;
}
list_item->next = list;
}
return ++list_len;
}
void graphics::draw(int delay) {
obj_list *list = NULL;
graphics_obj *obj = NULL;
list = list_head;
// Clear screen
SDL_FillRect(screen, NULL, clear_colour);
while (list != NULL) {
// Get object
obj = list->obj;
if (*obj->active) {
offset.x = *obj->pos_x;
offset.y = *obj->pos_y;
SDL_BlitSurface(obj->sprite, NULL, screen, &offset);
}
// Get next
list = list->next;
}
SDL_Flip(screen);
SDL_Delay(delay);
}
graphics::~graphics()
{
if (list_head != NULL) {
obj_list *list = NULL;
obj_list *prev = NULL;
list = list_head;
while (list != NULL) {
prev = list;
list = list->next;
delete prev;
}
}
}