-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
117 lines (103 loc) · 3.03 KB
/
main.cpp
File metadata and controls
117 lines (103 loc) · 3.03 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
#include "jm.cpp"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
// using std::cout;
// using std::endl;
// the number of pixels used to represent one pixle on the machine
#define SIZE 5
// geometry of your window
#define WIDTH 240 * SIZE
#define HEIGHT 136 * SIZE
void save_texture(const char *file_name, SDL_Renderer *renderer,
SDL_Texture *texture) {
SDL_Texture *target = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, texture);
int width, height;
// SDL_QueryTexture(texture, NULL, NULL, &width, &height);
SDL_QueryTexture(target, NULL, NULL, &width, &height);
// SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0,
// 0);
SDL_Surface *surface = SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0, 0, 0, 0);
SDL_RenderReadPixels(renderer, NULL, surface->format->format, surface->pixels,
surface->pitch);
IMG_SavePNG(surface, file_name);
SDL_FreeSurface(surface);
SDL_SetRenderTarget(renderer, target);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: ./main rom\n");
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO) != 0)
exit(1);
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
bool done = false;
JM machine = JM();
machine.load(argv[1]);
bool red, grn, blu, itn;
uint8_t color[3];
// int size = 2;
SDL_Rect rect;
// rect.w = size;
// rect.h = size;
rect.w = SIZE;
rect.h = SIZE;
SDL_Event event;
int count = 0;
int frame = 0;
SDL_Texture *texture;
while (!done) {
machine.run();
if (machine.DRAW) {
machine.DRAW = false;
for (int i = 0; i < 0x1FE0; ++i) {
for (int j = 3; j >= 0; --j) {
machine.nibbletrgb(color, machine.RAM[i] >> (j * 4));
SDL_SetRenderDrawColor(renderer, color[0], color[1], color[2],
SDL_ALPHA_OPAQUE);
int x = (i * 4 + (3 - j)) % 240;
int y = (i * 4 + (3 - j)) / 240;
// SDL_RenderDrawPoint(renderer, x, y);
// rect.x = x * size;
// rect.y = y * size;
rect.x = x * SIZE;
rect.y = y * SIZE;
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_RenderPresent(renderer);
SDL_Delay(5);
}
// save_texture("loser.png", renderer, texture);
count++;
if (machine.HALT)
break;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
done = true;
}
}
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_RenderCopy(renderer, texture, NULL, NULL);
if (renderer) {
SDL_DestroyRenderer(renderer);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();
return 0;
}