Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,32 @@ To run rNES just call:
The following options are supported:

```text
Use: ./rNES [-s <scale_factor>] [-d] [-h] <rom_file>
Use: ./rNES [-s <scale_factor>] [-f] [-d] [-h] <rom_file>

-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
-l, --license Show license
-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.
Expand Down
11 changes: 10 additions & 1 deletion docs/porting-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ 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);
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);
```
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions docs/thread-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
│
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.

Expand Down
66 changes: 61 additions & 5 deletions src/gui/sdl/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/include/hal/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ 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);
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);

Expand Down
31 changes: 24 additions & 7 deletions src/interface/posix/rNES.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <timeapi.h>
Expand Down Expand Up @@ -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 <scale_factor>] [-d] [-h] <rom_file>\n\n", progname);
log_info("Use: %s [-s <scale_factor>] [-f] [-d] [-h] <rom_file>\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(
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion test-framework/harness/null_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading