diff --git a/README.md b/README.md index 3a1596f..4eda5d4 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,10 @@ To run rNES just call: The following options are supported: ```text -Use: ./rNES [-s ] [-d] [-h] +Use: ./rNES [-s ] [-f] [-d] [-h] -s, --scale Set screen scale factor (default: 2) + -f, --fullscreen Enable full screen mode -d, --debug Show debug messages (it can be extremely slow!) -i, --info Just show ROM information (don't play the game) -v, --version Show version @@ -47,6 +48,21 @@ Use: ./rNES [-s ] [-d] [-h] -h, --help Show this help ``` +### Keyboard controls + +| Key | Action | +|-------------|-----------------------------| +| Arrow keys | D-pad | +| Z | A | +| X | B | +| A | Start | +| S | Select | +| F12 | Toggle full screen mode | +| Q | Quit | + +In full screen mode the picture keeps the NES aspect ratio and is letterboxed +to fit the display. + ## Windows (cross-compile) A static version for Windows can be cross-compiled on Linux using the MinGW-w64 toolchain. diff --git a/docs/porting-guide.md b/docs/porting-guide.md index bf52f72..11e33cb 100644 --- a/docs/porting-guide.md +++ b/docs/porting-guide.md @@ -102,7 +102,7 @@ The caller frees `*data`. ### GUI ```c -int gui_init(int scale); +int gui_init(int scale, int fullscreen); int gui_destroy(void); int gui_main_loop(void); void gui_set_pixel(int x, int y, RGB_t color); @@ -110,6 +110,7 @@ RGB_t gui_get_pixel(int x, int y); void gui_draw_text(int x, int y, const char *text, RGB_t color); int gui_get_scale(void); int gui_set_scale(int scale); +int gui_set_fullscreen(uint8_t fullscreen); int gui_is_running(void); uint8_t gui_read_joypad(int joypad); ``` @@ -216,9 +217,17 @@ error code. ### 3. Implement `gui_init` / `gui_destroy` - Open a framebuffer or window of `256*scale × 240*scale` pixels. +- If `fullscreen` is non-zero, switch to full screen mode (see + `gui_set_fullscreen`). - Inside `gui_init`, call `audio_init()` and `audio_play()`. - Return `0` on success, `-1` on failure. +`gui_set_fullscreen(1)` switches to full screen mode and `gui_set_fullscreen(0)` +returns to a window. Keep the 256×240 aspect ratio and letterbox the picture +rather than stretching it. The SDL port uses `SDL_WINDOW_FULLSCREEN_DESKTOP` +with `SDL_RenderSetLogicalSize()`. Platforms with a fixed display, where the +choice does not apply, may ignore the request and return `0`. + ### 4. Implement the pixel buffer (`gui_set_pixel` / `gui_get_pixel`) Maintain a local `RGB_t` array of size `256 × 240`. `gui_set_pixel` writes into diff --git a/docs/thread-model.md b/docs/thread-model.md index c84361e..4ad6e49 100644 --- a/docs/thread-model.md +++ b/docs/thread-model.md @@ -42,7 +42,7 @@ main() │ ├── pthread_create(th_gui) ──► gui_th_loop() │ │ - │ ├── gui_init(scale) ← opens window + audio device + │ ├── gui_init(scale, fs) ← opens window + audio device │ ├── sem_post(&wait_gui) ← unblocks main thread │ └── gui_main_loop() ← blocks here │ @@ -87,11 +87,11 @@ host CPU allows, relying solely on audio backpressure for real-time pacing. ```c /** GUI emulation thread (main routine) - * @param args Pointer to an integer variable with scale factor */ + * @param args Pointer to a struct _gui_args (scale and fullscreen) */ static void *gui_th_loop(void *args) { - int *scale = (int *)args; - if (gui_init(*scale) < 0) { + struct _gui_args *gargs = (struct _gui_args *)args; + if (gui_init(gargs->scale, gargs->fullscreen) < 0) { log_err("Could not initialize GUI!\n"); return NULL; } @@ -104,7 +104,7 @@ static void *gui_th_loop(void *args) `gui_main_loop()` is responsible for: -1. Polling `SDL_PollEvent` and updating the joypad bitmasks used by `gui_read_joypad()`. +1. Polling `SDL_PollEvent`, updating the joypad bitmasks used by `gui_read_joypad()`, and handling the F12 full screen toggle. 2. Uploading the pixel array (written by `gui_set_pixel`) into the SDL texture and calling `SDL_RenderPresent` once per frame. 3. Returning when the user closes the window or otherwise requests a quit. diff --git a/src/gui/sdl/gui.c b/src/gui/sdl/gui.c index 3c563e6..aa266e3 100644 --- a/src/gui/sdl/gui.c +++ b/src/gui/sdl/gui.c @@ -100,6 +100,8 @@ struct _RNES_SDL_GUI { int height; /** Video scale */ int scale; + /** Fullscreen mode */ + uint8_t fullscreen; /** Control keys */ uint8_t keys[16]; /** GUI's state */ @@ -126,14 +128,16 @@ const RGB_t C_BLUE = {.R = 0x00, .G = 0x00, .B = 0xff}; /** * Initialize GUI - * @param [in] Screen scale factor (if 0, set default scale) + * @param [in] scale Screen scale factor (0: set default scale) + * @param [in] fullscreen Full screen mode (0: windowed, 1: full screen) * @return 0 on success, -1 otherwise. */ -int gui_init(int scale) +int gui_init(int scale, int fullscreen) { rnes_gui.win = NULL; rnes_gui.texture = NULL; rnes_gui.renderer = NULL; + rnes_gui.fullscreen = fullscreen; rnes_gui.state = GUI_STOPPED; /* Setup Audio specs */ @@ -191,7 +195,7 @@ int gui_init(int scale) } /* Check scale */ - if (scale == 0) { + if (scale <= 0) { rnes_gui.width = SCREEN_WIDTH; rnes_gui.height = SCREEN_HEIGHT; rnes_gui.scale = 1; @@ -208,6 +212,18 @@ int gui_init(int scale) /* Create Hardware Accelerated Renderer */ rnes_gui.renderer = SDL_CreateRenderer(rnes_gui.win, -1, SDL_RENDERER_ACCELERATED); + if (rnes_gui.renderer == NULL) { + /* Fallback into software render if hardware is not available */ + rnes_gui.renderer = + SDL_CreateRenderer(rnes_gui.win, -1, SDL_RENDERER_SOFTWARE); + } + if (rnes_gui.renderer != NULL) { + /* Keep the aspect ratio and letterbox in fullscreen */ + SDL_RenderSetLogicalSize(rnes_gui.renderer, rnes_gui.width, + rnes_gui.height); + } + /* Set scaling hint to keep pixels sharp */ + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); /* Create Streaming Texture */ rnes_gui.texture = SDL_CreateTexture( rnes_gui.renderer, SDL_PIXELFORMAT_ARGB8888, @@ -217,16 +233,18 @@ int gui_init(int scale) rnes_gui.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT); - /* Set scaling hint to keep pixels sharp */ - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); if (rnes_gui.texture == NULL || rnes_gui.renderer == NULL || rnes_gui.text_texture == NULL) { SDL_PauseAudio(1); TTF_Quit(); SDL_Quit(); + return -1; } SDL_SetTextureBlendMode(rnes_gui.text_texture, SDL_BLENDMODE_BLEND); + /* Check/set full screen mode */ + gui_set_fullscreen(rnes_gui.fullscreen); + SDL_PauseAudio(0); return 0; } @@ -320,6 +338,16 @@ int gui_main_loop(void) case SDLK_q: quit = 1; break; + case SDLK_F12: + /* Toggle fullscreen mode */ + if (main_ev.type == SDL_KEYDOWN && !main_ev.key.repeat) { + if (rnes_gui.fullscreen) { + gui_set_fullscreen(0); + } else { + gui_set_fullscreen(1); + } + } + break; } } } @@ -390,6 +418,34 @@ int gui_set_scale(int scale) return 0; } +/** + * Enable or disable full screen mode + * + * Uses the desktop resolution and letterboxes the picture to keep the NES + * aspect ratio. The mouse cursor is hidden while in full screen mode. + * @param [in] fullscreen 0: windowed, otherwise full screen + * @return 0 on success, -1 otherwise. + */ +int gui_set_fullscreen(uint8_t fullscreen) +{ + uint32_t flags = 0; + uint32_t cursor = SDL_ENABLE; + + if (fullscreen) { + flags = SDL_WINDOW_FULLSCREEN_DESKTOP; + cursor = SDL_DISABLE; + } + + if (SDL_SetWindowFullscreen(rnes_gui.win, flags) < 0) { + log_err("Cannot change fullscreen mode: %s\n", SDL_GetError()); + return -1; + } + SDL_ShowCursor(cursor); + + rnes_gui.fullscreen = fullscreen; + return 0; +} + int gui_is_running(void) { if (rnes_gui.state == GUI_RUNNING) diff --git a/src/include/hal/hal.h b/src/include/hal/hal.h index 0f09f63..84ceee0 100644 --- a/src/include/hal/hal.h +++ b/src/include/hal/hal.h @@ -90,7 +90,7 @@ ssize_t load_file(const char *pathname, uint8_t **data); /* GUI functions */ -int gui_init(int scale); +int gui_init(int scale, int fullscreen); int gui_destroy(void); int gui_main_loop(void); void gui_set_pixel(int x, int y, RGB_t color); @@ -98,6 +98,7 @@ RGB_t gui_get_pixel(int x, int y); void gui_draw_text(int x, int y, const char *text, RGB_t color); int gui_get_scale(void); int gui_set_scale(int scale); +int gui_set_fullscreen(uint8_t fullscreen); int gui_is_running(void); uint8_t gui_read_joypad(int joypad); diff --git a/src/interface/posix/rNES.c b/src/interface/posix/rNES.c index 4d2ab14..0130f58 100644 --- a/src/interface/posix/rNES.c +++ b/src/interface/posix/rNES.c @@ -66,6 +66,12 @@ static int quit_emulation; /** Semaphore to wait for GUI initialization */ sem_t wait_gui; +/** Parameters to GUI thread */ +struct _gui_args { + int scale; + int fullscreen; +}; + /* Platform specific tuning */ #ifdef _WIN32 #include @@ -272,9 +278,11 @@ static void show_version(const char *progname) static void show_help(const char *progname) { show_version(progname); - log_info("Use: %s [-s ] [-d] [-h] \n\n", progname); + log_info("Use: %s [-s ] [-f] [-d] [-h] \n\n", + progname); log_info(" -s, --scale Set screen scale factor (default: %d)\n", DEFAULT_SCALE); + log_info(" -f, --fullscreen Enable full screen mode\n"); log_info( " -d, --debug Show debug messages (it can be extremely slow!)\n"); log_info( @@ -297,13 +305,13 @@ static void *emu_th_loop(void *args) /** * GUI emulation thread (main routine) - * @param args Pointer to an integer variable with scale factor + * @param args Pointer to a struct with the parameters for the GUI thread */ static void *gui_th_loop(void *args) { - int *scale = (int *)args; + struct _gui_args *gargs = (struct _gui_args *)args; - if (gui_init(*scale) < 0) { + if (gui_init(gargs->scale, gargs->fullscreen) < 0) { log_err("Could not initialize GUI!\n"); return NULL; } @@ -317,21 +325,24 @@ int main(int argc, char *const argv[]) { static struct option long_opts[] = {{"help", no_argument, NULL, 'h'}, {"scale", required_argument, NULL, 's'}, + {"fullscreen", no_argument, NULL, 'f'}, {"info", no_argument, NULL, 'i'}, {"debug", no_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, {"license", no_argument, NULL, 'l'}, {NULL, no_argument, NULL, 0}}; - const char *opt_string = "hs:idvl"; + const char *opt_string = "hs:fidvl"; char *romfile; - char debug, info; + char debug, info, fullscreen; int opt, optli, scale, ret; pthread_t th_gui, th_emu; pthread_attr_t attr_gui, attr_emu; + struct _gui_args gargs; /* Handle command line */ debug = 0; info = 0; + fullscreen = 0; scale = DEFAULT_SCALE; while ((opt = getopt_long(argc, argv, opt_string, long_opts, &optli)) != -1) { @@ -348,6 +359,10 @@ int main(int argc, char *const argv[]) debug = 1; break; + case 'f': + fullscreen = 1; + break; + case 'v': show_version(argv[0]); return EXIT_SUCCESS; @@ -386,6 +401,8 @@ int main(int argc, char *const argv[]) log_err("Scale factor must be a positive integer.\n\n"); return EXIT_FAILURE; } + gargs.scale = scale; + gargs.fullscreen = fullscreen; /* Load ROM file */ log_info("Loading ROM: %s ...\n", romfile); @@ -443,7 +460,7 @@ int main(int argc, char *const argv[]) /* Create and initialize GUI thread */ pthread_attr_init(&attr_gui); - pthread_create(&th_gui, &attr_gui, &gui_th_loop, &scale); + pthread_create(&th_gui, &attr_gui, &gui_th_loop, &gargs); /* Wait GUI to get ready */ sem_wait(&wait_gui); diff --git a/test-framework/harness/null_hal.c b/test-framework/harness/null_hal.c index 5c28d5e..564f71e 100644 --- a/test-framework/harness/null_hal.c +++ b/test-framework/harness/null_hal.c @@ -32,9 +32,10 @@ static int gui_running; const uint32_t *null_framebuffer(void) { return framebuffer; } -int gui_init(int scale) +int gui_init(int scale, int fullscreen) { (void)scale; + (void)fullscreen; memset(framebuffer, 0, sizeof(framebuffer)); gui_running = 1; return 0; @@ -90,6 +91,12 @@ int gui_set_scale(int scale) return 0; } +int gui_set_fullscreen(uint8_t fullscreen) +{ + (void)fullscreen; + return 0; +} + int gui_is_running(void) { return gui_running; } /* Injected controller-1 state (raw controller_reg_t byte); set by the runner