-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCJsMB_SDL2.cpp
More file actions
242 lines (185 loc) · 5.58 KB
/
CJsMB_SDL2.cpp
File metadata and controls
242 lines (185 loc) · 5.58 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
234
235
236
237
238
239
240
241
242
/**
* JsMobileBasic API implementation
* Copyright (c) PROPHESSOR 2018
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <iostream>
#include <cstdlib>
#include <ctime>
int Main();
bool Loop();
namespace JsMB {
#include <math.h>
#include "SDL2/SDL.h"
typedef struct {
unsigned int width ;
unsigned int height ;
char *title ;
SDL_Window *sdl_window ;
SDL_Renderer *sdl_renderer ;
} $_window_t;
typedef struct {
$_window_t *window;
bool DEBUG;
unsigned char color[3];
unsigned char bgcolor[3];
} $_JsMB_t;
$_JsMB_t $JsMobileBasic;
// TODO: Header file
// SDL
bool createWindow(const char *title, unsigned short width, unsigned short height) {
$JsMobileBasic.window->title = (char *) title;
$JsMobileBasic.window->width = width;
$JsMobileBasic.window->height = height;
if($JsMobileBasic.DEBUG)
std::cout << "createWindow(" << title
<< ", " << width
<< ", " << height
<< ")" << std::endl;
if($JsMobileBasic.DEBUG)
std::cout << "{ *title: \"" << $JsMobileBasic.window->title
<< "\", width: " << $JsMobileBasic.window->width
<< ", height: " << $JsMobileBasic.window->height
<< " }" << std::endl;
$JsMobileBasic.window->sdl_window = SDL_CreateWindow(title, 0, 0, width, height, SDL_WINDOW_SHOWN);
if($JsMobileBasic.window->sdl_window == 0x0) {
std::cout << "[ERROR]: SDL_CreateWindow Error #" << SDL_GetError() << std::endl;
return false;
} else if($JsMobileBasic.DEBUG) {
std::cout << "Initialize window - OK!" << std::endl;
}
$JsMobileBasic.window->sdl_renderer = SDL_CreateRenderer($JsMobileBasic.window->sdl_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if($JsMobileBasic.window->sdl_renderer == 0x0) {
std::cout << "[ERROR]: SDL_CreateRenderer Error #" << SDL_GetError() << std::endl;
return false;
} else if($JsMobileBasic.DEBUG) {
std::cout << "Initialize renderer - OK!" << std::endl;
}
return true;
}
void delay(int ms) {
SDL_Delay(ms);
}
// Get
unsigned short screenWidth() {
return $JsMobileBasic.window->width;
}
unsigned short screenHeight() {
return $JsMobileBasic.window->height;
}
// Math
/** From math.h
* sin
* cos
* tan
* pow
* exp
* log
* sqrt
* asin
* acos
* atan
* ceil
* floor
* abs
*/
double ctg(double x) {
return 1 / tan(x);
}
float random(double min, double max) {
return (float)(rand() % ((int) max * 100 + 1)) / 100 + min;
}
int random(int min, int max) {
return (rand() % (max - min)) + min;
}
// Graphics
bool setColor(unsigned char red, unsigned char green, unsigned char blue) {
$JsMobileBasic.color[0] = red;
$JsMobileBasic.color[1] = green;
$JsMobileBasic.color[2] = blue;
SDL_SetRenderDrawColor($JsMobileBasic.window->sdl_renderer, red, green, blue, 255);
return true;
}
bool setBgColor(unsigned char red, unsigned char green, unsigned char blue) {
$JsMobileBasic.bgcolor[0] = red;
$JsMobileBasic.bgcolor[1] = green;
$JsMobileBasic.bgcolor[2] = blue;
return false;
}
bool cls() {
unsigned char tmpcolor[3];
tmpcolor[0] = $JsMobileBasic.color[0];
tmpcolor[1] = $JsMobileBasic.color[1];
tmpcolor[2] = $JsMobileBasic.color[2];
setColor(0x0, 0x0, 0x0);
SDL_RenderClear($JsMobileBasic.window->sdl_renderer);
setColor(tmpcolor[0], tmpcolor[1], tmpcolor[2]);
return true;
}
bool repaint() {
SDL_RenderPresent($JsMobileBasic.window->sdl_renderer);
}
// Draw
bool drawRect(short x, short y, short width, short height) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
SDL_RenderDrawRect($JsMobileBasic.window->sdl_renderer, &rect);
return true;
}
bool fillRect(short x, short y, short width, short height) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
SDL_RenderDrawRect($JsMobileBasic.window->sdl_renderer, &rect);
SDL_RenderFillRect($JsMobileBasic.window->sdl_renderer, &rect);
return true;
}
bool fillScreen(unsigned char red, unsigned char green, unsigned char blue) {
unsigned char tmpcolor[3];
tmpcolor[0] = $JsMobileBasic.color[0];
tmpcolor[1] = $JsMobileBasic.color[1];
tmpcolor[2] = $JsMobileBasic.color[2];
setColor(red, green, blue);
fillRect(0, 0, screenWidth(), screenHeight());
setColor(tmpcolor[0], tmpcolor[1], tmpcolor[2]);
return true;
}
bool drawLine(short x1, short y1, short x2, short y2) {
SDL_RenderDrawLine($JsMobileBasic.window->sdl_renderer, x1, y1, x2, y2);
return true;
}
// Main
bool $Loop(); // Promise =D
int $Main() {
$JsMobileBasic.window = new $_window_t;
$JsMobileBasic.DEBUG = true;
if($JsMobileBasic.DEBUG)
std::cout << "CJsMB_SDL2 Loaded! main();" << std::endl;
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cout << "[ERROR]: SDL2 Init Error " << SDL_GetError() << std::endl;
return 1;
} else if($JsMobileBasic.DEBUG) {
std::cout << "Initialize SDL - OK!" << std::endl;
}
srand(time(0));
int mainres = Main();
if(mainres != 0) return mainres;
bool loopres = true;
while(loopres) {
loopres = Loop();
}
}
bool $Loop() {
return Loop();
}
} // namespace JsMB
int main() {
return JsMB::$Main();
}