-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_sdl_compat.h
274 lines (218 loc) · 4.49 KB
/
mc_sdl_compat.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
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
typedef uint8_t Uint8;
typedef uint16_t Uint16;
typedef uint32_t Uint32;
void *SDL_malloc(unsigned int size) {
return malloc(size);
}
void SDL_free(void *ptr) {
free(ptr);
}
extern void mc_sleep(void);
extern void turtle_x(int);
extern void turtle_y(int);
extern void turtle_z(int);
extern void turtle_set(int);
extern void turtle_copy(void);
extern void turtle_paste(void);
void mc_present(void) {
turtle_z(-20);
turtle_x(129);
turtle_y(1);
turtle_set(9);
}
typedef struct {
int x, y, w, h;
} SDL_Rect;
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} SDL_Color;
typedef struct {
Uint8 BitsPerPixel;
Uint8 BytesPerPixel;
} SDL_PixelFormat;
typedef struct {
SDL_PixelFormat *format;
int w, h;
int pitch;
void *pixels;
int locked;
SDL_Rect clip_rect;
} SDL_Surface;
void SDL_FreeSurface(SDL_Surface *surf) {
if (surf == NULL) {
return;
}
if (surf->pixels != NULL) { SDL_free(surf->pixels); }
if (surf->format != NULL) { SDL_free(surf->format); }
SDL_free(surf);
}
int SDL_SetPalette(SDL_Surface *surf, int flags, SDL_Color *pal, int firstcolor, int ncolors) {
return 0;
}
#define SDL_SRCCOLORKEY 1
int SDL_SetColorKey(SDL_Surface *surf, int yes, int key) {
return 0;
}
Uint8 keyState[20] = { 0 };
Uint8 *SDL_GetKeyState(int *numkeys) {
return keyState;
}
void SDL_Flip_screen() {
}
#define SCREEN_W 128
#define SCREEN_H 128
enum Block {
AIR,
COBBLESTONE,
GRANITE,
ANDESITE,
DIORITE,
LAPIS_BLOCK,
IRON_BLOCK,
GOLD_BLOCK,
DIAMOND_BLOCK,
REDSTONE_BLOCK,
EMERALD_BLOCK,
DIRT_BLOCK,
OAK_LOG_BLOCK,
OAK_LEAVES_BLOCK,
COAL_BLOCK,
};
const enum Block palette_colors[16] = {
COAL_BLOCK, LAPIS_BLOCK, GRANITE, OAK_LEAVES_BLOCK,
DIRT_BLOCK, COBBLESTONE, ANDESITE, IRON_BLOCK,
REDSTONE_BLOCK, GOLD_BLOCK, GOLD_BLOCK, EMERALD_BLOCK,
DIAMOND_BLOCK, ANDESITE, GRANITE, COBBLESTONE,
};
void SDL_SetPixel_screen_palette(int x, int y, unsigned char palette_idx) {
if (x < 0 || x >= 128) {
return;
}
if (y < 0 || y >= 128) {
return;
}
turtle_z(-20);
turtle_x(128 - x);
turtle_y(128 - y);
palette_idx %= 16;
turtle_set(palette_colors[palette_idx]);
}
void SDL_FillRect_screen_palette(SDL_Rect *rect, char palette_idx) {
int x, y, w, h;
if (rect) {
x = rect->x;
y = rect->y;
w = rect->w;
h = rect->h;
if (x < 0) {
w += x;
x = 0;
}
if (y < 0) {
h += y;
y = 0;
}
if (x + w > SCREEN_W) {
w -= (x + w - SCREEN_W);
}
if (y + h > SCREEN_H) {
h -= (y + h - SCREEN_H);
}
} else {
x = 0;
y = 0;
w = SCREEN_W;
h = SCREEN_H;
}
if (w <= 0 || h <= 0) {
return;
}
SDL_SetPixel_screen_palette(x, y, palette_idx);
turtle_copy();
for (int cur_y = y; cur_y < y + h; ++cur_y) {
turtle_y(128 - cur_y);
for (int cur_x = x; cur_x < x + w; ++cur_x) {
turtle_x(128 - cur_x);
turtle_paste();
}
}
}
#define SDL_INIT_AUDIO (1 << 0)
#define SDL_INIT_VIDEO (1 << 1)
#define SDL_INIT_GAMECONTROLLER (1 << 2)
int SDL_Init(Uint32 flags) {
return 0;
}
int SDL_InitSubSystem(Uint32 system) {
return 0;
}
#define SDL_SWSURFACE (1 << 0)
#define SDL_HWPALETTE (1 << 1)
SDL_Surface* SDL_CreateRGBSurface(
Uint32 flags, int width, int height, int depth,
Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
if (flags != SDL_SWSURFACE) {
// TODO:
return 0;
}
if (depth != 8 || Rmask != 0 || Gmask != 0 || Bmask != 0 || Amask != 0) {
// TODO:
return 0;
}
void *pixels = SDL_malloc(width * height);
if (pixels == 0) {
return 0;
}
SDL_Surface *surf = (SDL_Surface*)SDL_malloc(sizeof(SDL_Surface));
if (surf == 0) {
SDL_free(pixels);
return 0;
}
SDL_PixelFormat *format = (SDL_PixelFormat*)SDL_malloc(sizeof(SDL_PixelFormat));
if (format == 0) {
SDL_free(pixels);
SDL_free(surf);
}
format->BitsPerPixel = 8;
format->BytesPerPixel = 1;
surf->w = width;
surf->h = height;
surf->clip_rect.x = 0;
surf->clip_rect.y = 0;
surf->clip_rect.w = surf->w;
surf->clip_rect.h = surf->h;
surf->locked = 0;
surf->pitch = width;
surf->pixels = pixels;
surf->format = format;
return surf;
}
#define SDL_PHYSPAL (1 << 0)
#define SDL_LOGPAL (1 << 0)
const char *SDL_GetError() {
return "";
}
#define SDL_QUIT 0
#define SDL_KEYDOWN 1
#define SDL_KEYUP 2
typedef struct {
int type;
} SDL_Event;
#define SDLK_LEFT 0
#define SDLK_RIGHT 1
#define SDLK_UP 2
#define SDLK_DOWN 3
#define SDLK_z 4
#define SDLK_c 5
#define SDLK_n 6
#define SDLK_x 7
#define SDLK_v 8
#define SDLK_m 9
#define SDLK_F9 10
#include "mc_sdl_bmp.h"