libc8
CHIP-8 utility library
Loading...
Searching...
No Matches
graphics_sdl2.c File Reference
#include "../common.h"
#include "../graphics.h"
#include "exception.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <SDL2/SDL_mixer.h>
#include <stdint.h>

Macros

#define C8_RGB_R(i)   ((i >> 12) & 0xFF)
 
#define C8_RGB_G(i)   ((i >> 8) & 0xFF)
 
#define C8_RGB_B(i)   (i & 0xFF)
 
#define C8_AUDIO_CHANNEL   1
 
#define C8_AUDIO_SAMPLE_RATE   44100
 
#define C8_AUDIO_WAVE_FREQ   440
 
#define C8_AUDIO_WAVE_LENGTH   C8_AUDIO_SAMPLE_RATE / C8_AUDIO_WAVE_FREQ
 

Functions

int c8_sound_play (void)
 Start playing the sound.
 
int c8_sound_stop (void)
 Stop the sound playing.
 
int c8_deinit_graphics (void)
 Deinitialize the graphics library.
 
int c8_init_graphics (void)
 Initialize the graphics library.
 
int c8_render (C8_Display *display, int *colors)
 Render graphics.
 
int c8_tick (int *key)
 Process keypresses.
 

Detailed Description

Note
NOT EXPORTED

SDL2 graphics implementation. When SDL2 is defined, overrides weak graphics utility function definitions in graphics.c.

Macro Definition Documentation

◆ C8_AUDIO_CHANNEL

#define C8_AUDIO_CHANNEL   1

◆ C8_AUDIO_SAMPLE_RATE

#define C8_AUDIO_SAMPLE_RATE   44100

◆ C8_AUDIO_WAVE_FREQ

#define C8_AUDIO_WAVE_FREQ   440

◆ C8_AUDIO_WAVE_LENGTH

#define C8_AUDIO_WAVE_LENGTH   C8_AUDIO_SAMPLE_RATE / C8_AUDIO_WAVE_FREQ

◆ C8_RGB_B

#define C8_RGB_B (   i)    (i & 0xFF)

◆ C8_RGB_G

#define C8_RGB_G (   i)    ((i >> 8) & 0xFF)

◆ C8_RGB_R

#define C8_RGB_R (   i)    ((i >> 12) & 0xFF)

Function Documentation

◆ c8_deinit_graphics()

int c8_deinit_graphics ( void  )

Deinitialize the graphics library.

Deinitialize graphics system.

◆ c8_init_graphics()

int c8_init_graphics ( void  )

Initialize the graphics library.

Initialize graphics system.

Returns
0 if successful, error code otherwise.

◆ c8_render()

int c8_render ( C8_Display display,
int *  colors 
)

Render graphics.

Render the given display to the SDL2 window.

Parameters
displayC8_Display to render
colorscolors to render
Returns
0 on success, non-zero on failure

◆ c8_sound_play()

int c8_sound_play ( void  )

Start playing the sound.

Stop sound.

Returns
0 if successful, error code otherwise.

◆ c8_sound_stop()

int c8_sound_stop ( void  )

Stop the sound playing.

Play sound.

Returns
0 if successful, error code otherwise.

◆ c8_tick()

int c8_tick ( int *  key)

Process keypresses.

Grab current keypresses and delay execution to match clockspeed.

If a relevant key is pressed or released (see c8_keyMap in this file), this function will update key accordingly.

Parameters
keypointer to int arr of keys
Returns
-2 if quitting, -1 if no key was released, else returns value of key released.