This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL_Plotter.h
276 lines (219 loc) · 6.54 KB
/
SDL_Plotter.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* SDL_Plotter.h
*
* Created on: Jun 13, 2016
* Author: booth
*/
#ifndef SDL_PLOTTER_H_
#define SDL_PLOTTER_H_
//OSX Library
//#include <SDL2/SDL.h>
//#include <SDL2_mixer/SDL_mixer.h>
//#include <SDL2/SDL_thread.h>
//Windows Library
#include <SDL.h>
#include <SDL_mixer.h>
#include <string.h>
#include <iostream>
#include <string>
#include <string.h>
#include <map>
using namespace std;
const char UP_ARROW = 1;
const char DOWN_ARROW = 2;
const char LEFT_ARROW = 3;
const char RIGHT_ARROW = 4;
const int RED_SHIFT = 65536;
const int GREEN_SHIFT = 256;
const int BLUE_SHIFT = 1;
const int ALPHA_SHIFT = 16777216;
const int WHITE = 255;
const int MAX_THREAD = 100;
#define MUS_PATH "scratch.wav"
//sample threaded sound function
static int Sound(void *data);
struct param{
bool play;
bool running;
bool pause;
SDL_Thread* threadID;
SDL_cond *cond;
SDL_mutex *mut;
string name;
param(){
play = false;
running = false;
pause = false;
}
};
class SDL_Plotter{
private:
SDL_Texture * texture;
SDL_Renderer * renderer;
SDL_Window * window;
bool leftMouseButtonDown;
Uint32 * pixels;
const Uint8* currentKeyStates;
int row, col;
bool quit;
SDL_Event event;
//Sound Stuff
bool SOUND;
int soundCount;
map<string, param> soundMap;
public:
SDL_Plotter(int r=480, int c=640, bool WITH_SOUND = true){
row = r;
col = c;
leftMouseButtonDown = false;
quit = false;
SOUND = WITH_SOUND;
SDL_Init(SDL_INIT_AUDIO);
window = SDL_CreateWindow("SDL2 Pixel Drawing",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, col, row, 0);
renderer = SDL_CreateRenderer(window, -1, 0);
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, col, row);
pixels = new Uint32[col * row];
memset(pixels, WHITE, col * row * sizeof(Uint32));
//SOUND Thread Pool
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
soundCount = 0;
}
~SDL_Plotter(){
delete[] pixels;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void update(){
SDL_UpdateTexture(texture, NULL, pixels, col * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
bool getQuit(){
return quit;
}
bool kbhit(){
bool flag = false;
if(SDL_PollEvent(&event)){
if(event.type == SDL_KEYDOWN){
flag = true;
}
if( event.type == SDL_QUIT )
{
quit = true;
}
}
return flag;
}
char getKey(){
char key = '\0';
currentKeyStates = SDL_GetKeyboardState( NULL );
if(currentKeyStates[SDL_SCANCODE_A]) key = 'A';
if(currentKeyStates[SDL_SCANCODE_B]) key = 'B';
if(currentKeyStates[SDL_SCANCODE_C]) key = 'C';
if(currentKeyStates[SDL_SCANCODE_D]) key = 'D';
if(currentKeyStates[SDL_SCANCODE_E]) key = 'E';
if(currentKeyStates[SDL_SCANCODE_F]) key = 'F';
if(currentKeyStates[SDL_SCANCODE_G]) key = 'G';
if(currentKeyStates[SDL_SCANCODE_H]) key = 'H';
if(currentKeyStates[SDL_SCANCODE_I]) key = 'I';
if(currentKeyStates[SDL_SCANCODE_J]) key = 'J';
if(currentKeyStates[SDL_SCANCODE_K]) key = 'K';
if(currentKeyStates[SDL_SCANCODE_L]) key = 'L';
if(currentKeyStates[SDL_SCANCODE_M]) key = 'M';
if(currentKeyStates[SDL_SCANCODE_N]) key = 'N';
if(currentKeyStates[SDL_SCANCODE_O]) key = 'O';
if(currentKeyStates[SDL_SCANCODE_P]) key = 'P';
if(currentKeyStates[SDL_SCANCODE_Q]) key = 'Q';
if(currentKeyStates[SDL_SCANCODE_R]) key = 'R';
if(currentKeyStates[SDL_SCANCODE_S]) key = 'S';
if(currentKeyStates[SDL_SCANCODE_T]) key = 'T';
if(currentKeyStates[SDL_SCANCODE_U]) key = 'U';
if(currentKeyStates[SDL_SCANCODE_V]) key = 'V';
if(currentKeyStates[SDL_SCANCODE_W]) key = 'W';
if(currentKeyStates[SDL_SCANCODE_X]) key = 'X';
if(currentKeyStates[SDL_SCANCODE_Y]) key = 'Y';
if(currentKeyStates[SDL_SCANCODE_Z]) key = 'Z';
if(currentKeyStates[SDL_SCANCODE_1]) key = '1';
if(currentKeyStates[SDL_SCANCODE_2]) key = '2';
if(currentKeyStates[SDL_SCANCODE_3]) key = '3';
if(currentKeyStates[SDL_SCANCODE_4]) key = '4';
if(currentKeyStates[SDL_SCANCODE_5]) key = '5';
if(currentKeyStates[SDL_SCANCODE_6]) key = '6';
if(currentKeyStates[SDL_SCANCODE_7]) key = '7';
if(currentKeyStates[SDL_SCANCODE_8]) key = '8';
if(currentKeyStates[SDL_SCANCODE_9]) key = '9';
if(currentKeyStates[SDL_SCANCODE_0]) key = '0';
if(currentKeyStates[SDL_SCANCODE_SPACE]) key = ' ';
if(currentKeyStates[SDL_SCANCODE_DOWN]) key = DOWN_ARROW;
if(currentKeyStates[SDL_SCANCODE_UP]) key = UP_ARROW;
if(currentKeyStates[SDL_SCANCODE_LEFT]) key = LEFT_ARROW;
if(currentKeyStates[SDL_SCANCODE_RIGHT]) key = RIGHT_ARROW;
if(currentKeyStates[SDL_SCANCODE_RETURN]) key = SDL_SCANCODE_RETURN;
if(currentKeyStates[SDL_SCANCODE_ESCAPE]) quit = true;
return key;
}
void plotPixel(int x, int y, int r, int g, int b){
pixels[y * col + x] = RED_SHIFT*r + GREEN_SHIFT*g + BLUE_SHIFT*b;
}
void clear(){
memset(pixels, WHITE, col * row * sizeof(Uint32));
}
int getRow(){
return row;
}
int getCol(){
return col;
}
void initSound(string sound){
//int *threadReturnValue;
if(!soundMap[sound].running){
param* p = &soundMap[sound];
p->name = sound;
p->cond = SDL_CreateCond();
p->mut = SDL_CreateMutex();
p->threadID = SDL_CreateThread( Sound, sound.c_str(), (void*)p );
//p->threadID = SDL_CreateThread( Sound, "SoundThread", (void*)p );
//SDL_DetachThread(p->threadID);
}
}
void setQuit(bool flag){
this->quit = flag;
}
void playSound(string sound){
if(soundMap[sound].running){
SDL_CondSignal(soundMap[sound].cond);
}
}
void quitSound(string sound){
soundMap[sound].running = false;
SDL_CondSignal(soundMap[sound].cond);
}
void Sleep(int ms){
SDL_Delay(ms);
}
};
//Threaded Function
static int Sound(void *data){
param *p = (param*)data;
p->running = true;
Mix_Chunk *gScratch = NULL;
gScratch = Mix_LoadWAV( p->name.c_str() );
while(p->running){
SDL_mutexP( p->mut );
SDL_CondWait(p->cond, p->mut);
Mix_PlayChannel( -1, gScratch, 0 );
p->play = false;
SDL_mutexV(p->mut);
}
Mix_FreeChunk( gScratch );
p->running = false;
return 0;
}
#endif /* SDL_PLOTTER_H_ */