-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.cpp
81 lines (63 loc) · 1.66 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);
window = SDL_CreateWindow(caption, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, res_x, res_y, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
list_len = 0;
list_head = NULL;
list_curr = NULL;
}
int graphics::add_object(graphics_obj *obj)
{
obj_list *list = new obj_list;
list->obj = obj;
list->next = NULL;
// First item
if (list_head == NULL) {
list_head = list;
list_curr = list;
} else {
// Append to list
list_curr->next = list;
list_curr = list;
}
return ++list_len;
}
void graphics::draw() {
obj_list *list = NULL;
graphics_obj *obj = NULL;
list = list_head;
SDL_RenderClear(renderer);
while (list != NULL) {
// Get object
obj = list->obj;
if (*obj->active) {
offset.x = *obj->pos_x;
offset.y = *obj->pos_y;
offset.w = obj->size_x;
offset.h = obj->size_y;
SDL_RenderCopy(renderer, obj->texture, NULL, &offset);
}
// Get next
list = list->next;
}
SDL_RenderPresent(renderer);
}
graphics::~graphics()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
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;
}
}
}