-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
388 lines (313 loc) · 13.3 KB
/
Copy pathmain.py
File metadata and controls
388 lines (313 loc) · 13.3 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
import sys
import os
import ctypes
import sdl2
import numpy as np
# Añadir el directorio actual al path para importar pydmg
sys.path.insert(0, os.path.dirname(__file__))
from pydmg import GameBoy
# Importar SaveState de forma segura
try:
from pydmg.savestate import SaveState
SAVESTATE_AVAILABLE = True
except ImportError:
SAVESTATE_AVAILABLE = False
# Suprimir warnings de ALSA en Linux
try:
ERROR_HANDLER_FUNC = ctypes.CFUNCTYPE(
None, ctypes.c_char_p, ctypes.c_int,
ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p
)
c_error_handler = ERROR_HANDLER_FUNC(lambda *args: None)
asound = ctypes.cdll.LoadLibrary('libasound.so.2')
asound.snd_lib_error_set_handler(c_error_handler)
except:
pass
# Paletas de colores como arrays NumPy para lookup rápido
PALETTES = {
'dmg': np.array([[155, 188, 15], [139, 172, 15], [48, 98, 48], [15, 56, 15]], dtype=np.uint8),
'gray': np.array([[255, 255, 255], [192, 192, 192], [96, 96, 96], [0, 0, 0]], dtype=np.uint8),
'green': np.array([[224, 248, 208], [136, 192, 112], [52, 104, 86], [8, 24, 32]], dtype=np.uint8),
'pocket': np.array([[255, 255, 255], [170, 170, 170], [85, 85, 85], [0, 0, 0]], dtype=np.uint8),
}
class Emulator:
"""Emulador de Game Boy con SDL2"""
SCREEN_WIDTH = 160
SCREEN_HEIGHT = 144
SCALE = 4
def __init__(self):
# Inicializar SDL2
if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_AUDIO) < 0:
raise RuntimeError(f"SDL2 init error: {sdl2.SDL_GetError()}")
# Crear ventana
self.window = sdl2.SDL_CreateWindow(
b"Game Boy Emulator",
sdl2.SDL_WINDOWPOS_CENTERED,
sdl2.SDL_WINDOWPOS_CENTERED,
self.SCREEN_WIDTH * self.SCALE,
self.SCREEN_HEIGHT * self.SCALE,
sdl2.SDL_WINDOW_SHOWN
)
if not self.window:
raise RuntimeError(f"Window error: {sdl2.SDL_GetError()}")
# Crear renderer
self.renderer = sdl2.SDL_CreateRenderer(
self.window, -1,
sdl2.SDL_RENDERER_ACCELERATED | sdl2.SDL_RENDERER_PRESENTVSYNC
)
if not self.renderer:
raise RuntimeError(f"Renderer error: {sdl2.SDL_GetError()}")
# Crear textura para el framebuffer (RGB24)
self.texture = sdl2.SDL_CreateTexture(
self.renderer,
sdl2.SDL_PIXELFORMAT_RGB24,
sdl2.SDL_TEXTUREACCESS_STREAMING,
self.SCREEN_WIDTH,
self.SCREEN_HEIGHT
)
# Buffer de píxeles (RGB, 3 bytes por pixel) - contiguo en memoria
self.pixels = np.zeros((self.SCREEN_HEIGHT, self.SCREEN_WIDTH, 3), dtype=np.uint8, order='C')
# Game Boy
self.gameboy = None
self.rom_path = None
# Paleta actual
self.palette_names = list(PALETTES.keys())
self.palette_idx = 0
self.palette = PALETTES[self.palette_names[0]]
# Mapeo de teclas
self.keymap = {
sdl2.SDLK_RIGHT: 'right',
sdl2.SDLK_LEFT: 'left',
sdl2.SDLK_UP: 'up',
sdl2.SDLK_DOWN: 'down',
sdl2.SDLK_z: 'a',
sdl2.SDLK_x: 'b',
sdl2.SDLK_RETURN: 'start',
sdl2.SDLK_RSHIFT: 'select',
sdl2.SDLK_LSHIFT: 'select',
sdl2.SDLK_a: 'a',
sdl2.SDLK_s: 'b',
}
# Estado
self.running = False
self.paused = False
self.turbo = False
self.debug = False
# Save states
self.save_slot = 0
# FPS
self.fps_timer = sdl2.SDL_GetTicks()
self.frame_count = 0
self.fps = 0.0
def load_rom(self, path):
"""Carga una ROM"""
if not os.path.exists(path):
print(f"❌ ROM no encontrada: {path}")
return False
self.rom_path = path
self.gameboy = GameBoy()
self.gameboy.load_rom(path)
self._update_title()
return True
def _update_title(self):
"""Actualiza el título de la ventana"""
if self.rom_path:
rom_name = os.path.basename(self.rom_path)
title = f"Game Boy - {rom_name} [Slot {self.save_slot}]"
if self.paused:
title += " (PAUSA)"
if self.turbo:
title += " [TURBO]"
else:
title = "Game Boy Emulator"
sdl2.SDL_SetWindowTitle(self.window, title.encode('utf-8'))
def _save_state(self):
"""Guarda el estado actual"""
if not SAVESTATE_AVAILABLE:
print("⚠ Save states no disponibles")
return
if SaveState.save(self.gameboy, self.save_slot):
print(f"💾 Estado guardado en slot {self.save_slot}")
def _load_state(self):
"""Carga un estado guardado"""
if not SAVESTATE_AVAILABLE:
print("⚠ Save states no disponibles")
return
if SaveState.load(self.gameboy, self.save_slot):
print(f"📂 Estado cargado desde slot {self.save_slot}")
def handle_events(self):
"""Procesa eventos SDL2"""
event = sdl2.SDL_Event()
while sdl2.SDL_PollEvent(ctypes.byref(event)):
if event.type == sdl2.SDL_QUIT:
self.running = False
elif event.type == sdl2.SDL_KEYDOWN:
key = event.key.keysym.sym
# Controles del emulador
if key == sdl2.SDLK_ESCAPE:
self.running = False
elif key == sdl2.SDLK_p:
self.paused = not self.paused
self._update_title()
print("⏸ PAUSA" if self.paused else "▶ PLAY")
elif key == sdl2.SDLK_c:
self.palette_idx = (self.palette_idx + 1) % len(self.palette_names)
name = self.palette_names[self.palette_idx]
self.palette = PALETTES[name]
print(f"🎨 Paleta: {name}")
elif key == sdl2.SDLK_m:
self.gameboy.toggle_audio()
elif key == sdl2.SDLK_d:
self.debug = not self.debug
print(f"🔧 Debug: {'ON' if self.debug else 'OFF'}")
elif key == sdl2.SDLK_r:
print("🔄 Reset")
self.gameboy = GameBoy()
self.gameboy.load_rom(self.rom_path)
elif key == sdl2.SDLK_SPACE:
self.turbo = True
self._update_title()
# Save States
elif key == sdl2.SDLK_F5:
self._save_state()
elif key == sdl2.SDLK_F7:
self._load_state()
elif key == sdl2.SDLK_F6:
self.save_slot = (self.save_slot - 1) % 10
self._update_title()
print(f"📍 Slot {self.save_slot}")
elif key == sdl2.SDLK_F8:
self.save_slot = (self.save_slot + 1) % 10
self._update_title()
print(f"📍 Slot {self.save_slot}")
elif sdl2.SDLK_0 <= key <= sdl2.SDLK_9:
self.save_slot = key - sdl2.SDLK_0
self._update_title()
print(f"📍 Slot {self.save_slot}")
elif key == sdl2.SDLK_F1:
self._print_help()
# Controles del juego
elif key in self.keymap:
self.gameboy.press_button(self.keymap[key])
elif event.type == sdl2.SDL_KEYUP:
key = event.key.keysym.sym
if key == sdl2.SDLK_SPACE:
self.turbo = False
self._update_title()
elif key in self.keymap:
self.gameboy.release_button(self.keymap[key])
def _print_help(self):
"""Muestra la ayuda"""
print("""
╔═══════════════════════════════════════════════╗
║ AYUDA ║
╠═══════════════════════════════════════════════╣
║ JUEGO: ║
║ Flechas = D-Pad ║
║ Z / A = Botón A ║
║ X / S = Botón B ║
║ Enter = Start ║
║ Shift = Select ║
║ ║
║ EMULADOR: ║
║ P = Pausar ║
║ M = Mute/Unmute ║
║ C = Cambiar paleta ║
║ R = Reset ║
║ D = Debug mode ║
║ Space = Turbo (mantener) ║
║ ESC = Salir ║
║ ║
║ SAVE STATES: ║
║ F5 = Guardar estado ║
║ F7 = Cargar estado ║
║ F6 / F8 = Slot anterior/siguiente ║
║ 0-9 = Seleccionar slot ║
╚═══════════════════════════════════════════════╝
""")
def render(self, framebuffer):
"""Renderiza el framebuffer a la pantalla - OPTIMIZADO"""
# Convertir framebuffer a numpy array si es necesario
if isinstance(framebuffer, np.ndarray):
fb = framebuffer
else:
# Si es lista de listas, convertir a numpy
fb = np.asarray(framebuffer, dtype=np.uint8)
# Aplicar paleta usando indexación numpy (vectorizado)
# Esto reemplaza ~23,000 iteraciones con una sola operación
np.take(self.palette, fb, axis=0, out=self.pixels)
# Actualizar textura
sdl2.SDL_UpdateTexture(
self.texture,
None,
self.pixels.ctypes.data_as(ctypes.c_void_p),
self.SCREEN_WIDTH * 3 # pitch = width * bytes_per_pixel
)
# Renderizar
sdl2.SDL_RenderClear(self.renderer)
sdl2.SDL_RenderCopy(self.renderer, self.texture, None, None)
sdl2.SDL_RenderPresent(self.renderer)
def _update_fps(self):
"""Actualiza el contador de FPS"""
self.frame_count += 1
current = sdl2.SDL_GetTicks()
if current - self.fps_timer >= 1000:
self.fps = self.frame_count * 1000.0 / (current - self.fps_timer)
self.frame_count = 0
self.fps_timer = current
if self.debug:
print(f"FPS: {self.fps:.1f}")
def run(self):
"""Loop principal"""
if not self.gameboy:
print("❌ No hay ROM cargada")
return
self.running = True
print("\n🎮 Emulación iniciada (F1 = Ayuda)")
print(f" Slot: {self.save_slot} | F5=Guardar | F7=Cargar\n")
target_frame_time = 1000 // 60
try:
while self.running:
frame_start = sdl2.SDL_GetTicks()
self.handle_events()
if not self.paused:
# Ejecutar frames
frames = 4 if self.turbo else 1
for _ in range(frames):
fb = self.gameboy.run_frame()
self.render(fb)
self._update_fps()
# Control de timing (solo si no está en turbo)
if not self.turbo:
elapsed = sdl2.SDL_GetTicks() - frame_start
if elapsed < target_frame_time:
sdl2.SDL_Delay(target_frame_time - elapsed)
except KeyboardInterrupt:
print("\n⏹ Interrumpido")
finally:
self.cleanup()
def cleanup(self):
"""Limpia recursos"""
if self.gameboy:
self.gameboy.close()
if self.texture:
sdl2.SDL_DestroyTexture(self.texture)
if self.renderer:
sdl2.SDL_DestroyRenderer(self.renderer)
if self.window:
sdl2.SDL_DestroyWindow(self.window)
sdl2.SDL_Quit()
print("👋 Emulador cerrado")
def main():
print("╔" + "═" * 48 + "╗")
print("║ GAME BOY EMULATOR - PySDL2 ║")
print("╚" + "═" * 48 + "╝")
if len(sys.argv) < 2:
print("\nUso: python main.py <rom.gb>")
sys.exit(1)
emu = Emulator()
if emu.load_rom(sys.argv[1]):
emu.run()
if __name__ == "__main__":
main()