Skip to content

Commit

Permalink
Now this compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
cgokmen committed Nov 4, 2018
1 parent 0254bee commit 22a4500
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
3 changes: 3 additions & 0 deletions gameGraphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
// including the background and whatnot.
void fullDrawGame(Game *game) {
// TA-TODO: IMPLEMENT.
UNUSED(game);
}

// This function will be used to undraw (i.e. erase) things that might
// move in a frame. E.g. in a Snake game, erase the Snake, the food & the score.
void undrawGame(Game *game) {
// TA-TODO: IMPLEMENT.
UNUSED(game);
}

// This function will be used to draw things that might have moved in a frame.
// For example, in a Snake game, draw the snake, the food, the score.
void drawGame(Game *game) {
// TA-TODO: IMPLEMENT.
UNUSED(game);
}
5 changes: 5 additions & 0 deletions gameGraphics.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef GRAPHICS_SEEN
#define GRAPHICS_SEEN

#include "gameLogic.h"

// This function will be used to draw everything about the game
Expand All @@ -14,3 +17,5 @@ void drawGame(Game *game);

// If you have anything else you need accessible from outside the gameLogic.c
// file, you can add them here. You likely won't.

#endif
5 changes: 4 additions & 1 deletion gameLogic.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ void initializeGame(Game* game) {
// TA-TODO: Initialize everything that's part of this Game struct here.
// Suppose the struct contains random values, make sure everything gets
// the value it should have when the game begins.
UNUSED(game);
}

// TA-TODO: Add any process functions for sub-elements of your game here.
Expand All @@ -30,5 +31,7 @@ void processGame(Game *game, u32 keysPressedBefore, u32 keysPressedNow) {
*
* Do not do any drawing here.
*/

UNUSED(game);
UNUSED(keysPressedBefore);
UNUSED(keysPressedNow);
}
5 changes: 5 additions & 0 deletions gameLogic.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef LOGIC_SEEN
#define LOGIC_SEEN

#include "gba.h"

typedef struct {
Expand Down Expand Up @@ -39,3 +42,5 @@ void processGame(Game *game, u32 keysPressedBefore, u32 keysPressedNow);

// If you have anything else you need accessible from outside the gameLogic.c
// file, you can add them here. You likely won't.

#endif
33 changes: 21 additions & 12 deletions gba.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "gba.h"

u16 *videoBuffer = (u16 *)0x6000000;
volatile unsigned short *videoBuffer = (u16 *)0x6000000;
u32 vBlankCounter = 0;

void waitForVBlank() {
void waitForVBlank(void) {
// TA-TODO: IMPLEMENT

// Write a while loop that keeps going until we're NOT in vBlank anymore:
Expand All @@ -19,13 +19,7 @@ void waitForVBlank() {
}

static int __qran_seed= 42;
static int sqran(int seed) {
int old= __qran_seed;
__qran_seed= seed;
return old;
}

static int qran() {
static int qran(void) {
__qran_seed= 1664525*__qran_seed+1013904223;
return (__qran_seed>>16) & 0x7FFF;
}
Expand All @@ -36,37 +30,52 @@ int randint(int min, int max) {

void setPixel(int x, int y, u16 color) {
// TA-TODO: IMPLEMENT
UNUSED(x);
UNUSED(y);
UNUSED(color);
}

void drawRectDMA(int x, int y, int width, int height, volatile u16 color) {
// TA-TODO: IMPLEMENT
UNUSED(x);
UNUSED(y);
UNUSED(width);
UNUSED(height);
UNUSED(color);
}

void drawFullScreenImageDMA(u16 *image) {
// TA-TODO: IMPLEMENT
UNUSED(image);
}

void drawImageDMA(int x, int y, int width, int height, u16 *image) {
// TA-TODO: IMPLEMENT
UNUSED(x);
UNUSED(y);
UNUSED(width);
UNUSED(height);
UNUSED(image);
}

void fillScreenDMA(volatile u16 color) {
// TA-TODO: IMPLEMENT
UNUSED(color);
}

void drawChar(int col, int row, char ch, u16 color) {
for(int r = 0; r<8; r++) {
for(int c=0; c<6; c++) {
if(fontdata_6x8[OFFSET(r, c, 6) + ch*48]) {
setPixel(videoBuffer, col+c, row+r, color);
setPixel(col+c, row+r, color);
}
}
}
}

void drawString(int col, int row, char *str, u16 color) {
while(*str) {
drawChar(videoBuffer, col, row, *str++, color);
drawChar(col, row, *str++, color);
col += 6;
}
}
Expand All @@ -84,5 +93,5 @@ void drawCenteredString(int x, int y, int width, int height, char *str, u16 colo

int col = x + ((width - strWidth) >> 1);
int row = y + ((height - strHeight) >> 1);
drawString(videoBuffer, col, row, str, color);
drawString(col, row, str, color);
}
15 changes: 10 additions & 5 deletions gba.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef GBA_SEEN
#define GBA_SEEN

// ---------------------------------------------------------------------------
// USEFUL TYPEDEFS
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -112,12 +115,12 @@ extern u32 vblankCounter;
/**
* Runs a blocking loop until the start of next VBlank.
*/
void waitForVBlank();
void waitForVBlank(void);

// ---------------------------------------------------------------------------
// MISC
// ---------------------------------------------------------------------------
#define UNUSED_PARAM(param) ((void)((param)))
#define UNUSED(param) ((void)((param)))

/**
* Generates a pseudo-random number between min and max, inclusive.
Expand All @@ -136,10 +139,12 @@ void drawRectDMA(int x, int y, int width, int height, volatile u16 color);
void drawFullScreenImageDMA(u16 *image);
void drawImageDMA(int x, int y, int width, int height, u16 *image);
void fillScreenDMA(volatile u16 color);
void drawChar(int x, int y, char ch, u16 color);
void drawString(int x, int y, char *str, u16 color);
void drawCenteredString(int x, int y, int width, int height, char *str, u16 color);
void drawChar(int col, int row, char ch, u16 color);
void drawString(int col, int row, char *str, u16 color);
void drawCenteredString(int col, int row, int width, int height, char *str, u16 color);

/** Contains the pixels of each character from a 6x8 font */
// This is in the font.c file. You can replace the font if you want.
extern const unsigned char fontdata_6x8[12288];

#endif
12 changes: 6 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef enum {
GAMEOVER_NODRAW,
} GBAState;

int main() {
int main(void) {
GBAState state = START;
Game game;

Expand Down Expand Up @@ -47,22 +47,22 @@ int main() {
break;
case GAME_INIT:
// Initialize the game. Switch to the GAME state.
initializeGame(game);
initializeGame(&game);

// Draw the initial state of the game
fullDrawGame(game);
fullDrawGame(&game);

state = GAME;
break;
case GAME:
// Un-draw any potentially moving elements of the game
undrawGame(game);
undrawGame(&game);

// Process the game for one frame
processGame(game, previousButtons, currentButtons);
processGame(&game, previousButtons, currentButtons);

// Draw the current state of the game
drawGame(game);
drawGame(&game);

// Check if the game is over. If it is, then go to the gameover state.
if (game.gameOver) state = GAMEOVER;
Expand Down

0 comments on commit 22a4500

Please sign in to comment.