-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.cpp
More file actions
159 lines (151 loc) · 5.04 KB
/
Platform.cpp
File metadata and controls
159 lines (151 loc) · 5.04 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Platform.h"
#include <iostream>
using namespace std;
Platform::Platform(
char const * title,
int windowWidth, int windowHeight,
int imageWidth, int imageHeight,
int pitch
) : pitch(pitch)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(title, 0, 0, windowWidth, windowHeight, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
imageWidth,
imageHeight
);
}
Platform::~Platform() {
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Platform::update(const void * pixels) {
SDL_UpdateTexture(texture, nullptr, pixels, pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
bool Platform::processKeys(bool * keys) {
SDL_Event e;
while(SDL_PollEvent(&e)) { //loop over all queued keypresses
switch(e.type) {
case SDL_QUIT:
return true;
case SDL_KEYDOWN:
switch(e.key.keysym.scancode) {
case SDL_SCANCODE_X:
keys[0] = true;
break;
case SDL_SCANCODE_1:
keys[1] = true;
break;
case SDL_SCANCODE_2:
keys[2] = true;
break;
case SDL_SCANCODE_3:
keys[3] = true;
break;
case SDL_SCANCODE_Q:
keys[4] = true;
break;
case SDL_SCANCODE_W:
keys[5] = true;
break;
case SDL_SCANCODE_E:
keys[6] = true;
break;
case SDL_SCANCODE_A:
keys[7] = true;
break;
case SDL_SCANCODE_S:
keys[8] = true;
break;
case SDL_SCANCODE_D:
keys[9] = true;
break;
case SDL_SCANCODE_Z:
keys[0xA] = true;
break;
case SDL_SCANCODE_C:
keys[0xB] = true;
break;
case SDL_SCANCODE_4:
keys[0xC] = true;
break;
case SDL_SCANCODE_R:
keys[0xD] = true;
break;
case SDL_SCANCODE_F:
keys[0xE] = true;
break;
case SDL_SCANCODE_V:
keys[0xF] = true;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch(e.key.keysym.scancode) {
case SDL_SCANCODE_X:
keys[0] = false;
break;
case SDL_SCANCODE_1:
keys[1] = false;
break;
case SDL_SCANCODE_2:
keys[2] = false;
break;
case SDL_SCANCODE_3:
keys[3] = false;
break;
case SDL_SCANCODE_Q:
keys[4] = false;
break;
case SDL_SCANCODE_W:
keys[5] = false;
break;
case SDL_SCANCODE_E:
keys[6] = false;
break;
case SDL_SCANCODE_A:
keys[7] = false;
break;
case SDL_SCANCODE_S:
keys[8] = false;
break;
case SDL_SCANCODE_D:
keys[9] = false;
break;
case SDL_SCANCODE_Z:
keys[0xA] = false;
break;
case SDL_SCANCODE_C:
keys[0xB] = false;
break;
case SDL_SCANCODE_4:
keys[0xC] = false;
break;
case SDL_SCANCODE_R:
keys[0xD] = false;
break;
case SDL_SCANCODE_F:
keys[0xE] = false;
break;
case SDL_SCANCODE_V:
keys[0xF] = false;
break;
default:
break;
}
break;
}
}
return false;
}