-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdlwrapper.cpp
More file actions
233 lines (203 loc) · 6.79 KB
/
sdlwrapper.cpp
File metadata and controls
233 lines (203 loc) · 6.79 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
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
#include "sdlwrapper.h"
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <assert.h>
namespace paint
{
//global variables for SLD window and renderer
SDL_Window *SDLWin = 0;
SDL_Renderer *SDLRen = 0;
/*memorize colors set by setColor*/
Uint8 _r = 0, _g = 0, _b = 0;
void setColor (Uint8 r, Uint8 g, Uint8 b)
{
SDL_SetRenderDrawColor(SDLRen, r, g, b, SDL_ALPHA_OPAQUE);
_r = r;
_g = g;
_b = b;
}
class SdlInit
{
public:
SdlInit()
{
assert(SDL_Init(SDL_INIT_VIDEO) == 0);
TTF_Init();
SDLWin = SDL_CreateWindow("Hello World!", 100, 100, 800, 500, SDL_WINDOW_SHOWN);
assert(SDLWin != nullptr);
SDLRen = SDL_CreateRenderer(SDLWin, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
assert(SDLRen != nullptr);
SDL_Event e;
SDL_PollEvent(&e);
setColor(0, 0, 0);
SDL_RenderClear(SDLRen);
setColor(255, 255, 255);
}
~SdlInit()
{
SDL_DestroyRenderer(SDLRen);
SDL_DestroyWindow(SDLWin);
TTF_Quit();
SDL_Quit();
}
};
//global variable: constructor/destructor
//will handle SLD init/destroy
SdlInit sdlInit;
void drawTexture(SDL_Renderer *SDLRen, int x, int y, int w, int h, const char *file)
{
SDL_Texture *texture = IMG_LoadTexture(SDLRen, file);
/* int w, h;
SDL_QueryTexture(texture, NULL, NULL, &w, &h);*/
SDL_Rect texr;
texr.x = x;
texr.y = y;
texr.w = w;
texr.h = h;
SDL_RenderCopy(SDLRen, texture, NULL, &texr);
SDL_DestroyTexture(texture);
}
void drawPixel(int x, int y)
{
SDL_RenderDrawPoint(SDLRen, x, y);
}
void drawLine(int x1, int y1, int x2, int y2)
{
SDL_RenderDrawLine(SDLRen, x1, y1, x2, y2);
}
void drawFillRect(int x, int y, int w, int h) \
{
SDL_Rect r = {x, y, w, h};
SDL_RenderFillRect(SDLRen, &r);
}
void drawFile (int x, int y, int w, int h, const char* file)
{
drawTexture(SDLRen, x, y, w, h, file);
}
void updateGraphics()
{
SDL_RenderPresent(SDLRen);
}
void drawText (int x, int y, int size, const char* text)
{
TTF_Font *font = TTF_OpenFont("cpppainter/images/roboto.ttf", size);
assert(font != nullptr);
SDL_Color color = {_r, _g, _b};
SDL_Surface *surface = TTF_RenderText_Solid(font,text,color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(SDLRen, surface);
assert(surface != nullptr);
assert(texture != nullptr);
int texW = 0;
int texH = 0;
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
SDL_Rect dstrect = {x, y, texW, texH};
SDL_RenderCopy(SDLRen, texture, NULL, &dstrect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
TTF_CloseFont(font);
}
void waitKeypress(int key)
{
SDL_Event e;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_KEYDOWN && e.key.keysym.sym == key) {
quit = true;
}
}
}
}
void drawFillCircle(int x, int y, int radius)
{
int offsetx, offsety, d;
int status;
offsetx = 0;
offsety = radius;
d = radius -1;
status = 0;
while (offsety >= offsetx) {
status += SDL_RenderDrawLine(SDLRen, x - offsety, y + offsetx,
x + offsety, y + offsetx);
status += SDL_RenderDrawLine(SDLRen, x - offsetx, y + offsety,
x + offsetx, y + offsety);
status += SDL_RenderDrawLine(SDLRen, x - offsetx, y - offsety,
x + offsetx, y - offsety);
status += SDL_RenderDrawLine(SDLRen, x - offsety, y - offsetx,
x + offsety, y - offsetx);
if (status < 0) {
status = -1;
break;
}
if (d >= 2*offsetx) {
d -= 2*offsetx + 1;
offsetx +=1;
}
else if (d < 2 * (radius - offsety)) {
d += 2 * offsety - 1;
offsety -= 1;
}
else {
d += 2 * (offsety - offsetx - 1);
offsety -= 1;
offsetx += 1;
}
}
}
namespace turtle
{
Pen p = {0,0,false,0};
void down()
{
p.down = true;
}
void up()
{
p.down = false;
}
void forward(double distance)
{
double rad = p.angle * 3.14159265358979323846 / 180.0;
double newX = p.x + distance * cos(rad);
double newY = p.y + distance * sin(rad);
if(p.down)
{
drawLine(static_cast<int>(p.x), static_cast<int>(p.y), static_cast<int>(newX), static_cast<int>(newY));
}
p.x = newX;
p.y = newY;
}
void backward(double distance)
{
left(180);
forward(distance);
right(180);
}
void left(double a)
{
p.angle -= a;
if(p.angle < 0)
p.angle += 360;
}
void right(double a)
{
p.angle += a;
if(p.angle >= 360)
p.angle -= 360;
}
void turnto(double a)
{
p.angle = a;
}
void jump(double x, double y)
{
p.x = x;
p.y = y;
}
}
}