Skip to content

Commit

Permalink
Feature #19 - Adding graphical display. (closes #19) (#34)
Browse files Browse the repository at this point in the history
* Most basic graphical library addition.

gpu.c and gpu.h use Allegro library to draw a black window with ROM
title as window title.
gpu.c has provisions for future additions of drawing logic.

Probably will be overhauled later, as it effectively ignores the V-Blank
interval.

* Special R/W (unfinished), proper gpu step and LCD STAT updating.

Palette memory to be found. It seems we lack information about CGB vs
DMG ROMs (Address 143). Scanline drawing logic still to be done.

GPU divided into GPU and DISPLAY, where DISPLAY handles the graphical
library. It should allow for easier change of graphical library, if need
arises.

* General cleanup. Future issues now separated more clearly.

Modes separated to an enum for clarity. Now int.h enum is prefixed by
module name to solve naming problems.

* Update build.yml

Include Allegro lib in build action.

* Code now uses stdbool.h instead of its own definition.

This allows for compatibility with other libraries.

* mzal's requested changes

static keyword added for module variables.
Includes and enums moved to .c if not needed in .h.
Unneeded functions removed.
Enums now explicitely numbered.
checking for window closing now called directly in main.
More complex defines now encapsulated.

* Second round of reviews implemented.

Fixed inconsistent spacing in display.c.
Recognised interrupts now named in request_interrupt().
main.c display_get_closed_status() call now uses shorter logic check.
Unneccessary comments in display.c removed.
Curly braces added to gpu.c elses when ifs use them.
Counter reset in GPU now uses -=_CYCLES_PER_SCANLINE.
Unneccessary white symbols removed from main.c

Co-authored-by: galeblon <32581949+galeblon@users.noreply.github.com>
  • Loading branch information
Diamond-Dust and galeblon committed Aug 10, 2020
1 parent 9ebbb31 commit 481f719
Show file tree
Hide file tree
Showing 12 changed files with 612 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Install Allegro
run: sudo apt-get update && sudo apt-get install liballegro-acodec5-dev liballegro-audio5-dev liballegro-image5-dev liballegro-dialog5-dev liballegro-ttf5-dev liballegro-physfs5-dev
- uses: actions/checkout@v2
- name: make
run: make
Expand Down
5 changes: 3 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CC = gcc
CFLAGS = -g -Wall -Wextra -pedantic -O0
INCL = -I./include
SRCS = cpu.c logger.c regs.c rom.c ints.c main.c mem.c debug.c
SRCS = mem.c cpu.c logger.c regs.c rom.c ints.c gpu.c display.c main.c debug.c
PKG = $$(pkg-config --libs allegro-5)
OBJS = $(SRCS:.c=.o)
BIN = gbc

Expand All @@ -11,7 +12,7 @@ gbc_debug: CFLAGS += -DDEBUG
gbc_debug: gbc

gbc: $(OBJS)
$(CC) $(CFLAGS) $(INCL) -o $(BIN) $(OBJS)
$(CC) $(CFLAGS) $(INCL) -o $(BIN) $(OBJS) $(PKG)

.c.o:
$(CC) $(CFLAGS) $(INCL) -c $< -o $@
Expand Down
121 changes: 121 additions & 0 deletions src/display.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include<allegro5/allegro5.h>
#include"cpu.h"
#include"display.h"
#include"logger.h"

static ALLEGRO_DISPLAY *g_display = NULL;
static ALLEGRO_TIMER *g_timer = NULL;
static ALLEGRO_EVENT_QUEUE *g_close_event_queue = NULL;
static ALLEGRO_EVENT_QUEUE *g_event_queue = NULL;
static ALLEGRO_EVENT g_event;


static void _display_error(enum logger_log_type type, char *title, char *message)
{
char *full_message = logger_get_msg_buffer();
snprintf(
full_message,
LOG_MESSAGE_MAX_SIZE,
"[DISPLAY MODULE] %s\n",
message
);
logger_log(
type,
title,
full_message
);
}

void display_prepare(float frequency, char * rom_title)
{
if(!al_init()) {
_display_error(
LOG_FATAL,
"ALLEGRO INIT",
"FAILED TO INITIALISE ALLEGRO."
);
return;
}

g_timer = al_create_timer(frequency);
if (!g_timer) {
_display_error(
LOG_FATAL,
"ALLEGRO TIMER",
"FAILED TO CREATE A TIMER."
);
return;
}

g_display = al_create_display( 160 * SCALING_FACTOR, 144 * SCALING_FACTOR );
if(!g_display) {
_display_error(
LOG_FATAL,
"ALLEGRO DISPLAY",
"FAILED TO INITIALISE DISPLAY."
);
return;
}

g_event_queue = al_create_event_queue();
if (!g_event_queue) {
_display_error(
LOG_FATAL,
"ALLEGRO QUEUE",
"FAILED TO CREATE EVENT QUEUE."
);
return;
}

g_close_event_queue = al_create_event_queue();
if (!g_close_event_queue) {
_display_error(
LOG_FATAL,
"ALLEGRO QUEUE",
"FAILED TO CREATE EVENT QUEUE."
);
return;
}

al_register_event_source(
g_close_event_queue,
al_get_display_event_source(g_display)
);
al_register_event_source(
g_event_queue,
al_get_timer_event_source(g_timer)
);

al_start_timer(g_timer);

// Display a black screen, set the title
al_set_window_title(g_display, rom_title);
al_clear_to_color( al_map_rgb(0, 0, 0) );
al_flip_display();
}


bool display_get_closed_status(void)
{
// Fetch the event (if one exists)
bool event_exists = al_get_next_event(g_close_event_queue, &g_event);

if(event_exists)
// Handle the event
switch (g_event.type) {
case ALLEGRO_EVENT_DISPLAY_CLOSE:
return true;
default:
return false;
}
else
return false;
}


void display_destroy(void)
{
al_destroy_display(g_display);
al_destroy_event_queue(g_event_queue);
al_destroy_event_queue(g_close_event_queue);
}
Loading

0 comments on commit 481f719

Please sign in to comment.