-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem.c
More file actions
79 lines (67 loc) · 2.28 KB
/
system.c
File metadata and controls
79 lines (67 loc) · 2.28 KB
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
#include "system.h"
//RENDERING SUBSYSTEM
void sys_render_update(struct entities *_entities, struct sprites *_sprites, SDL_Renderer *renderer)
{
//UI/HUD elements defined as render components without position
//Object sprites defined as render components + position
unsigned int entity;
//struct cmp_position *pos;
//struct cmp_position *render;
for(entity = 0; entity < ENTITY_COUNT; ++entity)
{
if((_entities->component_mask[entity] & CMP_RENDER) == CMP_RENDER)
{
//TODO: use get_sprite here with the sprite database and draw it with SDL_RenderCopy()
int sprite = get_sprite(_sprites, renderer, _entities->renders[entity].name);
//fill out position rect if it contains a position component, otherwise use defaults.
int w, h;
SDL_Rect pos_rect;
pos_rect.x = 0;
pos_rect.y = 0;
if((_entities->component_mask[entity] & CMP_POSITION) == CMP_POSITION)
{
pos_rect.x = _entities->positions[entity].x;
pos_rect.y = _entities->positions[entity].y;
}
SDL_QueryTexture(_sprites->textures[sprite], NULL, NULL, &w, &h);
pos_rect.w = w;
pos_rect.h = h;
SDL_RenderCopy(renderer, _sprites->textures[sprite], NULL, &pos_rect);
}
}
}
void sys_render_print_info(struct entities *_entities)
{
unsigned int entity;
struct cmp_position *pos;
struct cmp_render *render;
for(entity = 0; entity < ENTITY_COUNT; ++entity)
{
if((_entities->component_mask[entity] & (CMP_POSITION | CMP_RENDER)) == (CMP_POSITION | CMP_RENDER))
{
pos = &(_entities->positions[entity]);
render = &(_entities->renders[entity]);
printf("%s at %f, %f\n", render->name, pos->x, pos->y);
}
}
}
//TODO: Possibly classify system.c into system_player.c or system_ai.c or whatever and split it into functions (but make there only one header, system.h)
//INPUT SUBSYSTEM
void sys_input_update(struct entities *_entities, const Uint8 *key)
{
int entity;
for(entity = 0; entity < ENTITY_COUNT; ++entity)
{
if((_entities->component_mask[entity] & CMP_INPUT_PLAYER) == CMP_INPUT_PLAYER)
{
if(key[SDL_SCANCODE_UP])
_entities->positions[entity].y-=2.0f;
if(key[SDL_SCANCODE_DOWN])
_entities->positions[entity].y+=2.0f;
if(key[SDL_SCANCODE_LEFT])
_entities->positions[entity].x-=2.0f;
if(key[SDL_SCANCODE_RIGHT])
_entities->positions[entity].x+=2.0f;
}
}
}