static void HandleKeyEvent(SDL.SDL_Keycode keyCode, Chip8 core, bool pressed) { switch (keyCode) { case SDL.SDL_Keycode.SDLK_1: core.SetKey(1, pressed); break; case SDL.SDL_Keycode.SDLK_2: core.SetKey(2, pressed); break; case SDL.SDL_Keycode.SDLK_3: core.SetKey(3, pressed); break; case SDL.SDL_Keycode.SDLK_4: core.SetKey(12, pressed); break; case SDL.SDL_Keycode.SDLK_a: core.SetKey(4, pressed); break; case SDL.SDL_Keycode.SDLK_z: core.SetKey(5, pressed); break; case SDL.SDL_Keycode.SDLK_e: core.SetKey(6, pressed); break; case SDL.SDL_Keycode.SDLK_r: core.SetKey(13, pressed); break; case SDL.SDL_Keycode.SDLK_q: core.SetKey(7, pressed); break; case SDL.SDL_Keycode.SDLK_s: core.SetKey(8, pressed); break; case SDL.SDL_Keycode.SDLK_d: core.SetKey(9, pressed); break; case SDL.SDL_Keycode.SDLK_f: core.SetKey(14, pressed); break; case SDL.SDL_Keycode.SDLK_w: core.SetKey(10, pressed); break; case SDL.SDL_Keycode.SDLK_x: core.SetKey(0, pressed); break; case SDL.SDL_Keycode.SDLK_c: core.SetKey(11, pressed); break; case SDL.SDL_Keycode.SDLK_v: core.SetKey(15, pressed); break; } }
static unsafe void Main(string[] args) { Chip8 core = new Chip8(); if (args.Length != 1) { Console.WriteLine("Usage: c8emu <ROM file name>"); return; } core.LoadROM(LoadFile(args[0])); if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) != 0) { Console.WriteLine("Failed to initialize SDL."); return; } emulatorWindow = SDL.SDL_CreateWindow("c8emu", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, 256, 128, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); if (emulatorWindow == null) { Console.WriteLine("Failed to create the window."); SDL.SDL_Quit(); return; } windowSurface = (SDL.SDL_Surface *)SDL.SDL_GetWindowSurface(emulatorWindow); screenSurface = (SDL.SDL_Surface *)SDL.SDL_CreateRGBSurface(SDL.SDL_SWSURFACE, 64, 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); if (screenSurface == null) { Console.WriteLine("Failed to create the screen surface."); SDL.SDL_DestroyWindow(emulatorWindow); SDL.SDL_Quit(); return; } core.DisplayUpdate += DisplayUpdate; //Console.Clear(); while (true) { core.FrameAdvance(); SDL.SDL_Event e; while (SDL.SDL_PollEvent(out e) != 0) { switch (e.type) { case SDL.SDL_EventType.SDL_WINDOWEVENT: if (e.window.windowEvent == SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE) { SDL.SDL_FreeSurface((IntPtr)screenSurface); SDL.SDL_DestroyWindow(emulatorWindow); SDL.SDL_Quit(); return; } break; case SDL.SDL_EventType.SDL_KEYDOWN: HandleKeyEvent(e.key.keysym.sym, core, true); break; case SDL.SDL_EventType.SDL_KEYUP: HandleKeyEvent(e.key.keysym.sym, core, false); break; } } SDL.SDL_Delay(16); } }