-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
331 lines (293 loc) · 10.3 KB
/
util.cpp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "cpu.h"
#include "memmap.h"
#include "rom.h"
#include "util.h"
#include<SDL2/SDL.h>
#ifndef __CYGWIN__
#include<minizip/unzip.h>
#endif
#include<iostream>
#include<fstream>
#include<cassert>
#include<png.h>
#ifdef DEBUG
#define ASSERT assert
#else
#define ASSERT //assert
#endif
namespace util {
#ifndef PPU_ONLY
bool process_events(cpu * proc, memmap * bus) {
SDL_Event event;
unsigned int newx,newy;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN: /* Handle a KEYDOWN event */
//printf("util::Keydown\n");
if(event.key.keysym.scancode==SDL_SCANCODE_Q||
(event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_RCTRL))||
(event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_LCTRL))) {
SDL_Quit();
return false;
}
else if(event.key.keysym.scancode==SDL_SCANCODE_T) {
proc->toggle_trace();
bus->keydown(event.key.keysym.scancode);
}
else {
bus->keydown(event.key.keysym.scancode);
}
break;
case SDL_KEYUP: /* Handle a KEYUP event*/
//printf("util::Keyup\n");
bus->keyup(event.key.keysym.scancode);
break;
case SDL_WINDOWEVENT:
//printf("util::Window event: ");
switch(event.window.event) {
case SDL_WINDOWEVENT_SHOWN:
//printf("shown\n");
break;
case SDL_WINDOWEVENT_HIDDEN:
//printf("hidden\n");
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
//printf("resized\n");
newx=event.window.data1;
newy=event.window.data2;
if(newx > 0 && newy > 0) {
//changed=true;
}
//ppui.resize(newx,newy);
bus->win_resize(newx, newy);
break;
case SDL_WINDOWEVENT_EXPOSED:
//printf("exposed\n");
//ppui.resize(1,1);
break;
case SDL_WINDOWEVENT_CLOSE:
//printf("closed\n");
SDL_Quit();
return false;
break;
default:
//printf("something else (%d)\n", event.window.event);
break;
}
break;
case SDL_QUIT:
//printf("util::SDL Quit triggered\n");
//SDL_PauseAudio(true);
SDL_Quit();
return false;
break;
case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: case SDL_MOUSEWHEEL:
//printf("Mouse event\n");
break;
case SDL_TEXTINPUT:
//printf("util::SDL TextInput\n");
break;
case SDL_KEYMAPCHANGED:
//printf("util::SDL KeymapChanged triggered\n");
break;
case SDL_AUDIODEVICEADDED:
//printf("util::SDL Audio Device Added\n");
break;
default: /* Report an unhandled event */
//printf("util::I don't know what this event is (%d)! Flushing it.\n", event.type);
SDL_FlushEvent(event.type);
break;
}
}
return true;
}
#endif
#ifndef __CYGWIN__
int unzip(const std::string& zip_filename, Vect<uint8_t>& output, size_t min_size, size_t max_size) {
unzFile f = unzOpen(zip_filename.c_str());
if(!f) {
//printf("Bleh2.\n");
return 2;
}
unz_global_info gi;
if(unzGetGlobalInfo(f,&gi) != UNZ_OK) {
//printf("Bleh3.\n");
return 3;
}
//printf("File seems to contain %ld file entries.\n", gi.number_entry);
if(unzGoToFirstFile(f) != UNZ_OK) {
//printf("Bleh4.\n");
return 4;
}
unz_file_info fi;
char filename[256];
if(unzGetCurrentFileInfo(f,&fi,filename,256,NULL,0,NULL,0) != UNZ_OK) {
//printf("Bleh5.\n");
return 5;
}
//printf("Filename: %s\nCompression method: %ld\nCompressed size: %ld\nExtracted size: %ld\n", filename, fi.compression_method, fi.compressed_size, fi.uncompressed_size);
if(fi.uncompressed_size < min_size || fi.uncompressed_size > max_size) {
unzClose(f);
return 8;
}
if(unzOpenCurrentFile(f) != UNZ_OK) {
//printf("Bleh6.\n");
return 6;
}
output.resize(fi.uncompressed_size);
size_t read_bytes = 0;
if((read_bytes = unzReadCurrentFile(f,reinterpret_cast<char *>(&output[0]),fi.uncompressed_size)) != fi.uncompressed_size) {
//printf("Bleh7. Got return of %ld\n", read_bytes);
return 7;
}
unzCloseCurrentFile(f);
unzClose(f);
return 0;
}
#endif
int read(const std::string& filename, Vect<uint8_t>& output, size_t min_size, size_t max_size) {
std::ifstream in(filename.c_str());
if(in.is_open()) {
size_t size = 0;
in.seekg(0, std::ios::end);
size = in.tellg();
if(size < min_size || size > max_size) {
in.close();
return 1;
}
in.seekg(0, std::ios::beg);
//printf("Opened %s, found a file of %ld bytes.\n", filename.c_str(), size);
output.resize(size);
in.read(reinterpret_cast<char *>(&output[0]), size);
in.close();
}
else {
fprintf(stderr, "Couldn't open %s.\n", filename.c_str());
return 2;
}
return 0;
}
bool reinit_sdl_screen(SDL_Window ** screen, SDL_Renderer ** renderer, SDL_Texture ** texture, unsigned int xres, unsigned int yres) {
if(screen && *screen) {
SDL_DestroyWindow(*screen);
*screen = NULL;
}
if(texture && *texture) {
//SDL_DestroyTexture(*texture);
//*texture = NULL;
}
if(renderer && *renderer) {
//SDL_DestroyRenderer(*renderer);
//*renderer = NULL;
}
/* Initialize the SDL library */
*screen = SDL_CreateWindow("KhedGB",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
xres*2, yres*2,
SDL_WINDOW_RESIZABLE);
if ( *screen == NULL ) {
fprintf(stderr, "lcd::Couldn't set %dx%dx32 video mode: %s\nStarting without video output.\n", xres*2, yres*2, SDL_GetError());
return false;
}
SDL_SetWindowMinimumSize(*screen, xres, yres);
*renderer = SDL_CreateRenderer(*screen, -1, SDL_RENDERER_ACCELERATED/*|SDL_RENDERER_PRESENTVSYNC*/);
//*renderer = SDL_CreateRenderer(*screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
//*renderer = SDL_CreateRenderer(*screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_PRESENTVSYNC);
//*renderer = SDL_CreateRenderer(*screen, -1, SDL_RENDERER_SOFTWARE/*|SDL_RENDERER_PRESENTVSYNC*/);
SDL_SetRenderDrawBlendMode(*renderer,SDL_BLENDMODE_BLEND);
if(!(*renderer)) {
fprintf(stderr, "lcd::Couldn't create a renderer: %s\nStarting without video output.\n", SDL_GetError());
SDL_DestroyWindow(*screen);
*screen = NULL;
return false;
}
*texture = SDL_CreateTexture(*renderer, SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,160,144);
if(!(*texture)) {
fprintf(stderr, "lcd::Couldn't create a texture: %s\nStarting without video output.\n", SDL_GetError());
SDL_DestroyRenderer(*renderer);
*renderer = NULL;
SDL_DestroyWindow(*screen);
*screen = NULL;
return false;
}
return true;
}
void destroy_sdl_screen() {
}
int clamp(int low, int val, int high) {
if(val < low) val = low;
if(val > high) val = high;
return val;
}
std::string to_hex_string(uint32_t val, int length) {
ASSERT(length <= 8);
char buf[10];
char fmt[10];
std::snprintf(fmt,10,"%%0%dx",length);
std::snprintf(buf,10,fmt,val);
std::string out(buf);
return out;
}
void setRGB(png_bytep r, uint8_t * i) {
*r = ((i[0]<<6)|(i[1]<<4)|(i[2]<<2)|i[3]);
}
int output_png(std::string& filename, int width, int height, Vect<uint8_t>& image) {
int code = 0;
FILE *fp = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep row = NULL;
// Open file for writing (binary mode)
fp = fopen(filename.c_str(), "wb");
if (fp == NULL) {
fprintf(stderr, "Could not open file %s for writing\n", filename.c_str());
code = 1;
goto finalise;
}
// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "Could not allocate write struct\n");
code = 1;
goto finalise;
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "Could not allocate info struct\n");
code = 1;
goto finalise;
}
// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during png creation\n");
code = 1;
goto finalise;
}
png_init_io(png_ptr, fp);
// Write header (8 bit colour depth)
png_set_IHDR(png_ptr, info_ptr, width, height,
2, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// Allocate memory for one row (3 bytes per pixel - RGB)
row = (png_bytep) malloc(width/4 * sizeof(png_byte));
// Write image data
int x, y;
for (y=0 ; y<height ; y++) {
for (x=0 ; x<width/4 ; x++) {
setRGB(&(row[x]), &(image[y*width + x*4]));
}
png_write_row(png_ptr, row);
}
// End write
png_write_end(png_ptr, NULL);
finalise:
if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
if (row != NULL) free(row);
return code;
}
}