-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.ixx
More file actions
214 lines (184 loc) · 5.16 KB
/
main.ixx
File metadata and controls
214 lines (184 loc) · 5.16 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
// Stephane Duguay et Jean-Sebastien Ross
// Tutorial: https://austinmorlan.com/posts/chip8_emulator/
// Test ROMs: https://github.com/dmatlack/chip8/tree/master/roms/games
import <iostream>;
import <string>;
import <clocale>;
import <cassert>;
import <chrono>;
import <unordered_map>;
#pragma warning( push )
#pragma warning( disable : 26819 )
#include <SDL.h>
#pragma warning( pop )
import VideoBuffer;
import Processeur;
import Memoire;
using namespace std;
//------------------------------------------------------------
// chip8 spec
static const int ECRAN_LARGEUR = 64;
static const int ECRAN_HAUTEUR = 32;
static const int MEMOIRE_TAILLE_OCTETS = 4096;
//------------------------------------------------------------
static const int SDL_RESOLUTION_LARGEUR = 1024;
static const int SDL_RESOLUTION_HAUTEUR = 512;
static const std::unordered_map<SDL_Keycode, octet> SDL_CLES
{
{ SDLK_1, 0x0 },
{ SDLK_2, 0x1 },
{ SDLK_3, 0x2 },
{ SDLK_4, 0x3 },
{ SDLK_q, 0x4 },
{ SDLK_w, 0x5 },
{ SDLK_e, 0x6 },
{ SDLK_r, 0x7 },
{ SDLK_a, 0x8 },
{ SDLK_s, 0x9 },
{ SDLK_d, 0xA },
{ SDLK_f, 0xB },
{ SDLK_z, 0xC },
{ SDLK_x, 0xD },
{ SDLK_c, 0xE },
{ SDLK_v, 0xF },
};
//------------------------------------------------------------
struct SDLEnv
{
SDL_Window* window{ nullptr };
SDL_Renderer* renderer{ nullptr };
bool ok() const { return window != nullptr && renderer != nullptr; }
};
//------------------------------------------------------------
SDLEnv init()
{
SDLEnv sdlEnv;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
cout << "Erreur d'initialisation de la librairie SDL: " << SDL_GetError() << endl;
return {};
}
sdlEnv.window = SDL_CreateWindow( "chip8",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SDL_RESOLUTION_LARGEUR,
SDL_RESOLUTION_HAUTEUR, 0);
if (sdlEnv.window)
{
sdlEnv.renderer = SDL_CreateRenderer(sdlEnv.window, -1, SDL_RENDERER_ACCELERATED);
}
return sdlEnv;
}
//------------------------------------------------------------
void render(SDL_Renderer& renderer, IVideoBuffer& video)
{
SDL_SetRenderDrawColor(&renderer, 0, 0, 0, 255); // Fond noir
SDL_RenderClear(&renderer);
SDL_SetRenderDrawColor(&renderer, 255, 255, 255, 255); // Pixel blanc
const float pixelRatioX{ static_cast<float>(SDL_RESOLUTION_LARGEUR) / static_cast<float>(ECRAN_LARGEUR) };
const float pixelRatioY{ static_cast<float>(SDL_RESOLUTION_HAUTEUR) / static_cast<float>(ECRAN_HAUTEUR) };
for (int y = 0; y < ECRAN_HAUTEUR; ++y)
{
for (int x = 0; x < ECRAN_LARGEUR; ++x)
{
if (video.pixel(x, y))
{
SDL_Rect rect{ static_cast<int>(x * pixelRatioX), static_cast<int>(y * pixelRatioY), pixelRatioX, pixelRatioY };
SDL_RenderFillRect(&renderer, &rect);
}
}
}
SDL_RenderPresent(&renderer);
}
bool maj(IProcesseur& processeur)
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
return false;
}
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
{
return false;
}
else
{
const auto itCle{ SDL_CLES.find(event.key.keysym.sym) };
if (itCle != SDL_CLES.end())
{
processeur.appuyerToucheClavier(itCle->second);
}
}
}
else if (event.type == SDL_KEYUP)
{
const auto itCle{ SDL_CLES.find(event.key.keysym.sym) };
if (itCle != SDL_CLES.end())
{
processeur.relacherToucheClavier(itCle->second);
}
}
}
return true;
}
//------------------------------------------------------------
void go(SDLEnv& env, IProcesseur & processeur, IVideoBuffer& video)
{
assert(env.ok());
const float majFrequenceHz{ 60.0f };
const float majTempsMs{ 1000.0f / majFrequenceHz };
auto dernierTick{ std::chrono::high_resolution_clock::now() };
while (env.ok() && maj(processeur))
{
const auto maintenant{ std::chrono::high_resolution_clock::now() };
const float delta{ std::chrono::duration<float, std::chrono::milliseconds::period>(maintenant - dernierTick).count() };
if( delta < majTempsMs )
continue;
processeur.tick();
render(*(env.renderer), video);
dernierTick = maintenant;
}
}
//------------------------------------------------------------
void bye(SDLEnv& env)
{
if (env.renderer)
SDL_DestroyRenderer(env.renderer);
if (env.window)
SDL_DestroyWindow(env.window);
SDL_Quit();
env = {};
}
//------------------------------------------------------------
int main(int argc, char* argv[])
{
setlocale(0, ""); // 0 = LC_ALL
if (argc != 2)
{
cout << "chip8cpp v0.99" << endl;
cout << "SVP spécifier le fichier de ROM à lancer." << endl;
cout << argv[0] << " FICHIER_ROM" << endl;
return -1;
}
SDLEnv env = init();
if (!env.ok())
{
return -2;
}
VideoBuffer video(ECRAN_LARGEUR, ECRAN_HAUTEUR);
Memoire memoire(MEMOIRE_TAILLE_OCTETS);
Processeur processeur(memoire, video);
if (!processeur.charger(argv[1]))
{
cout << "Erreur lors de l'exécution de la ROM. Fichier invalide." << endl;
return -2;
}
go(env, processeur, video);
cout << "ROM exécutée avec succès." << endl;
bye(env);
return 0;
}