-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-sdl1.py
189 lines (145 loc) · 6.3 KB
/
main-sdl1.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import collections
import itertools
import ctypes
import atexit
import argparse
import sdl
import junknes
import ines
import movie
NES_W = 256
NES_H = 240
SCALE = 2
WIN_W = SCALE * NES_W
WIN_H = SCALE * NES_H
AUDIO_FREQ = 44100
AUDIO_FORMAT = sdl.AUDIO_S16SYS
AUDIO_CHANNELS = 1
AUDIO_SAMPLES = 4096
FPS = 60
InputMapEntry = collections.namedtuple("InputMapEntry", ("key", "port", "button"))
INPUT_MAP = tuple(itertools.starmap(InputMapEntry, (
(sdl.SDLK_d, 0, junknes.JUNKNES_JOY_R),
(sdl.SDLK_a, 0, junknes.JUNKNES_JOY_L),
(sdl.SDLK_s, 0, junknes.JUNKNES_JOY_D),
(sdl.SDLK_w, 0, junknes.JUNKNES_JOY_U),
(sdl.SDLK_e, 0, junknes.JUNKNES_JOY_T),
(sdl.SDLK_q, 0, junknes.JUNKNES_JOY_S),
(sdl.SDLK_x, 0, junknes.JUNKNES_JOY_B),
(sdl.SDLK_z, 0, junknes.JUNKNES_JOY_A),
)))
PaletteRgb = junknes.JunknesRgb * 0x40
PALETTE_MASTER = PaletteRgb(*itertools.starmap(junknes.JunknesRgb, (
( 0x74, 0x74, 0x74 ), ( 0x24, 0x18, 0x8C ), ( 0x00, 0x00, 0xA8 ), ( 0x44, 0x00, 0x9C ),
( 0x8C, 0x00, 0x74 ), ( 0xA8, 0x00, 0x10 ), ( 0xA4, 0x00, 0x00 ), ( 0x7C, 0x08, 0x00 ),
( 0x40, 0x2C, 0x00 ), ( 0x00, 0x44, 0x00 ), ( 0x00, 0x50, 0x00 ), ( 0x00, 0x3C, 0x14 ),
( 0x18, 0x3C, 0x5C ), ( 0x00, 0x00, 0x00 ), ( 0x00, 0x00, 0x00 ), ( 0x00, 0x00, 0x00 ),
( 0xBC, 0xBC, 0xBC ), ( 0x00, 0x70, 0xEC ), ( 0x20, 0x38, 0xEC ), ( 0x80, 0x00, 0xF0 ),
( 0xBC, 0x00, 0xBC ), ( 0xE4, 0x00, 0x58 ), ( 0xD8, 0x28, 0x00 ), ( 0xC8, 0x4C, 0x0C ),
( 0x88, 0x70, 0x00 ), ( 0x00, 0x94, 0x00 ), ( 0x00, 0xA8, 0x00 ), ( 0x00, 0x90, 0x38 ),
( 0x00, 0x80, 0x88 ), ( 0x00, 0x00, 0x00 ), ( 0x00, 0x00, 0x00 ), ( 0x00, 0x00, 0x00 ),
( 0xFC, 0xFC, 0xFC ), ( 0x3C, 0xBC, 0xFC ), ( 0x5C, 0x94, 0xFC ), ( 0xCC, 0x88, 0xFC ),
( 0xF4, 0x78, 0xFC ), ( 0xFC, 0x74, 0xB4 ), ( 0xFC, 0x74, 0x60 ), ( 0xFC, 0x98, 0x38 ),
( 0xF0, 0xBC, 0x3C ), ( 0x80, 0xD0, 0x10 ), ( 0x4C, 0xDC, 0x48 ), ( 0x58, 0xF8, 0x98 ),
( 0x00, 0xE8, 0xD8 ), ( 0x78, 0x78, 0x78 ), ( 0x00, 0x00, 0x00 ), ( 0x00, 0x00, 0x00 ),
( 0xFC, 0xFC, 0xFC ), ( 0xA8, 0xE4, 0xFC ), ( 0xC4, 0xD4, 0xFC ), ( 0xD4, 0xC8, 0xFC ),
( 0xFC, 0xC4, 0xFC ), ( 0xFC, 0xC4, 0xD8 ), ( 0xFC, 0xBC, 0xB0 ), ( 0xFC, 0xD8, 0xA8 ),
( 0xFC, 0xE4, 0xA0 ), ( 0xE0, 0xFC, 0xA0 ), ( 0xA8, 0xF0, 0xBC ), ( 0xB0, 0xFC, 0xCC ),
( 0x9C, 0xFC, 0xF0 ), ( 0xC4, 0xC4, 0xC4 ), ( 0x00, 0x00, 0x00 ), ( 0x00, 0x00, 0x00 )
)))
class SDLError(Exception):
def __init__(self, msg):
msg_all = "{}: {}".format(msg, sdl.SDL_GetError().decode(errors="replace"))
super().__init__(msg_all)
def error(msg):
sys.exit(msg)
def get_input():
inputs = [0, 0]
sdl.SDL_PumpEvents()
keys = sdl.SDL_GetKeyState(None)
for m in INPUT_MAP:
if keys[m.key]: inputs[m.port] |= m.button
return inputs
def draw(blit, surf, buf):
# SDL_MUSTLOCK() が使えないのでとりあえず無条件でロック
# TODO: ただのソフトウェアサーフェスならロック不要?
if sdl.SDL_LockSurface(surf) != 0: raise SDLError("SDL_LockSurface()")
junknes.junknes_blit_do(blit, buf, surf.contents.pixels, SCALE)
sdl.SDL_UnlockSurface(surf)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("ines")
parser.add_argument("--movie")
return parser.parse_args()
def main():
args = parse_args()
prg, chr_, mirror = ines.ines_split(open(args.ines, "rb"))
movie_ = movie.fm2_read(open(args.movie, "r")) if args.movie else []
if(sdl.SDL_Init(sdl.SDL_INIT_VIDEO | sdl.SDL_INIT_TIMER) != 0):
raise SDLError("SDL_Init()")
atexit.register(sdl.SDL_Quit)
screen = sdl.SDL_SetVideoMode(WIN_W, WIN_H, 32, sdl.SDL_SWSURFACE)
if not screen: raise SDLError("SDL_SetVideoMode()")
assert screen.contents.format.contents.BytesPerPixel == 4
blit = junknes.junknes_blit_create(PALETTE_MASTER, junknes.JUNKNES_PIXEL_XRGB8888)
if not blit: error("junknes_blit_create() failed")
mixer = junknes.junknes_mixer_create(AUDIO_FREQ, AUDIO_SAMPLES, FPS)
if not mixer: error("junknes_mixer_create() failed")
want = sdl.SDL_AudioSpec()
want.freq = AUDIO_FREQ
want.format = AUDIO_FORMAT
want.channels = AUDIO_CHANNELS
want.samples = AUDIO_SAMPLES
want.callback = ctypes.cast(junknes.junknes_mixer_pull_sdl, sdl.SDL_AudioCallback)
want.userdata = ctypes.cast(mixer, ctypes.c_void_p)
have = sdl.SDL_AudioSpec()
if sdl.SDL_OpenAudio(ctypes.byref(want), ctypes.byref(have)) != 0:
raise SDLError("SDL_OpenAudio() failed")
if want.freq != have.freq or\
want.format != have.format or\
want.channels != have.channels: error("spec changed")
prg_ary = (0x8000*ctypes.c_uint8).from_buffer_copy(prg)
chr_ary = (0x2000*ctypes.c_uint8).from_buffer_copy(chr_)
nes = junknes.junknes_create(prg_ary, chr_ary, mirror)
sdl.SDL_PauseAudio(0)
start_ms = sdl.SDL_GetTicks()
frame = 0
running = True
while running:
ev = sdl.SDL_Event()
while sdl.SDL_PollEvent(ctypes.byref(ev)):
if (ev.type == sdl.SDL_QUIT or
(ev.type == sdl.SDL_KEYDOWN and ev.key.keysym.sym == sdl.SDLK_ESCAPE)):
running = False
if movie_:
cmd, inputs = movie_.pop(0)
if cmd & movie.COMMAND_HARDRESET:
junknes.junknes_hardreset(nes)
if cmd & movie.COMMAND_SOFTRESET:
junknes.junknes_softreset(nes)
else:
inputs = get_input()
for i, value in enumerate(inputs):
junknes.junknes_set_input(nes, i, value)
junknes.junknes_emulate_frame(nes)
sound = junknes.JunknesSound()
junknes.junknes_sound(nes, ctypes.byref(sound))
junknes.junknes_mixer_push(mixer, ctypes.byref(sound))
draw(blit, screen, junknes.junknes_screen(nes))
sdl.SDL_UpdateRect(screen, 0, 0, 0, 0)
frame += 1
if frame == 1000:
end_ms = sdl.SDL_GetTicks()
if end_ms == start_ms: end_ms += 1
print("fps: {:.2f}".format(1000.0 * frame / (end_ms-start_ms)))
start_ms = end_ms
frame = 0
sdl.SDL_Delay(7)
junknes.junknes_destroy(nes)
sdl.SDL_CloseAudio()
junknes.junknes_mixer_destroy(mixer)
junknes.junknes_blit_destroy(blit)
if __name__ == "__main__": main()