Skip to content

Commit

Permalink
Added new sprites for player, updated character and joystick classes …
Browse files Browse the repository at this point in the history
…to work together and move the player.
  • Loading branch information
DPriceDev committed Jul 17, 2020
1 parent 1aabfba commit 2568653
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(SRC
src/main.c
src/level_one.c
src/joystick_handler.c
src/boot/rom_head.c)
src/boot/rom_head.c src/character.c)

project(Megadrive-Maze)

Expand Down
29 changes: 28 additions & 1 deletion inc/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,37 @@
#define MEGADRIVE_MAZE_CHARACTER_H

#include <genesis.h>
#include "resource.h"

#include "joystick_handler.h"

enum LookDirection {
LOOK_LEFT,
LOOK_UP,
LOOK_RIGHT,
LOOK_DOWN
};

struct MovementVector {
int mX;
int mY;
};

struct Character {
Sprite* mSprite;
Sprite* mWalkingSprite;
Sprite* mIdleDownSprite;
Sprite* mIdleUpSprite;
enum LookDirection mLookDirection;
int mX, mY;
struct MovementVector mMovementVector;
};

struct Character *initCharacter();

void characterTick();

void updatePosition();

void characterJoystick(struct JoystickAction* joystickAction);

#endif //MEGADRIVE_MAZE_CHARACTER_H
20 changes: 20 additions & 0 deletions inc/joystick_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@

#include <genesis.h>

enum ButtonAction {
PRESSED,
RELEASED
};

enum Button {
LEFT = BUTTON_LEFT,
RIGHT = BUTTON_RIGHT,
UP = BUTTON_UP,
DOWN = BUTTON_DOWN
};

struct JoystickAction {
enum Button mButton;
enum ButtonAction mAction;
};

void joystickHandler(u16 joystick, u16 changed, u16 state);
void joystickOneHandler(u16 changed, u16 state);
void joystickTwoHandler(u16 changed, u16 state);

void setJoystickOneFunction(void (*functionPointer)(struct JoystickAction*));
struct JoystickAction* createJoystickAction(enum Button button, enum ButtonAction action);

#endif //MEGADRIVE_MAZE_JOYSTICK_HANDLER_H
3 changes: 3 additions & 0 deletions res/resource.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef _RES_RESOURCE_H_
#define _RES_RESOURCE_H_

extern const SpriteDefinition PlayerWalkingSprite;
extern const SpriteDefinition PlayerIdleDownSprite;
extern const SpriteDefinition PlayerIdleUpSprite;
extern const Image tilemap;

#endif // _RES_RESOURCE_H_
5 changes: 4 additions & 1 deletion res/resource.res
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
IMAGE tilemap "tilemaps/tilemap.bmp" 0
IMAGE tilemap "tilemaps/tilemap.bmp" 0
SPRITE PlayerWalkingSprite "sprites/PlayerWalkingHorizontalSprite.bmp" 1 1 FAST 15
SPRITE PlayerIdleDownSprite "sprites/PlayerIdleDownSprite.bmp" 1 1 FAST 15
SPRITE PlayerIdleUpSprite "sprites/PlayerIdleUpSprite.bmp" 1 1 FAST 15
Binary file added res/sprites/PlayerIdleDownSprite.bmp
Binary file not shown.
Binary file added res/sprites/PlayerIdleUpSprite.bmp
Binary file not shown.
Binary file added res/sprites/PlayerWalkingHorizontalSprite.bmp
Binary file not shown.
101 changes: 101 additions & 0 deletions src/character.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Created by urban on 17/07/2020.
//

#include "character.h"
#include "string.h"

struct Character *character;

struct Character *initCharacter() {
character = MEM_alloc(sizeof *character);
character->mX = 7 * 8;
character->mY = 7 * 8;
character->mIdleDownSprite = SPR_addSprite(&PlayerIdleDownSprite, character->mX, character->mY, TILE_ATTR(PAL1, 1, 0, 0));
character->mWalkingSprite = SPR_addSprite(&PlayerWalkingSprite, -8, -8, TILE_ATTR(PAL1, 1, 0, 0));
character->mIdleUpSprite = SPR_addSprite(&PlayerIdleUpSprite, -8, -8, TILE_ATTR(PAL1, 1, 0, 0));
struct MovementVector vector = {0, 0 };
character->mMovementVector = vector;
character->mLookDirection = LOOK_DOWN;
return character;
}

void characterJoystick(struct JoystickAction *joystickAction) {
switch (joystickAction->mButton) {
case LEFT:
if(joystickAction->mAction == PRESSED) {
character->mMovementVector.mX = -1;
} else {
character->mMovementVector.mX = 0;
}
break;
case RIGHT:
if(joystickAction->mAction == PRESSED) {
character->mMovementVector.mX = 1;
} else {
character->mMovementVector.mX = 0;
}
break;
case UP:
if(joystickAction->mAction == PRESSED) {
character->mMovementVector.mY = -1;
} else {
character->mMovementVector.mY = 0;
}
break;
case DOWN:
if(joystickAction->mAction == PRESSED) {
character->mMovementVector.mY = 1;
} else {
character->mMovementVector.mY = 0;
}
break;
}
}

void characterTick() {
updatePosition();
}

void setLookDirectionFromMovementVector() {
if(character->mMovementVector.mX > 0) {
character->mLookDirection = LOOK_RIGHT;
} else if(character->mMovementVector.mX < 0) {
character->mLookDirection = LOOK_LEFT;
} else if(character->mMovementVector.mY < 0) {
character->mLookDirection = LOOK_UP;
} else {
character->mLookDirection = LOOK_DOWN;
}
}

void updatePosition() {
character->mX += character->mMovementVector.mX;
character->mY += character->mMovementVector.mY;
setLookDirectionFromMovementVector();

switch (character->mLookDirection) {
case LOOK_LEFT:
SPR_setHFlip(character->mWalkingSprite, 0);
SPR_setPosition(character->mWalkingSprite, character->mX, character->mY);
SPR_setPosition(character->mIdleUpSprite, -8, -8);
SPR_setPosition(character->mIdleDownSprite, -8, -8);
break;
case LOOK_UP:
SPR_setPosition(character->mIdleUpSprite, character->mX, character->mY);
SPR_setPosition(character->mIdleDownSprite, -8, -8);
SPR_setPosition(character->mWalkingSprite, -8, -8);
break;
case LOOK_RIGHT:
SPR_setHFlip(character->mWalkingSprite, 1);
SPR_setPosition(character->mWalkingSprite, character->mX, character->mY);
SPR_setPosition(character->mIdleDownSprite, -8, -8);
SPR_setPosition(character->mIdleUpSprite, -8, -8);
break;
case LOOK_DOWN:
SPR_setPosition(character->mIdleDownSprite, character->mX, character->mY);
SPR_setPosition(character->mIdleUpSprite, -8, -8);
SPR_setPosition(character->mWalkingSprite, -8, -8);
break;
}
}
36 changes: 25 additions & 11 deletions src/joystick_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@

#include <genesis.h>

void (*joystickOneFunPointer)(struct JoystickAction*);

void setJoystickOneFunction(void (*functionPointer)(struct JoystickAction*)) {
joystickOneFunPointer = functionPointer;
}

struct JoystickAction* createJoystickAction(enum Button button, enum ButtonAction action) {
struct JoystickAction* joystickAction = MEM_alloc(sizeof *joystickAction);
joystickAction->mAction = action;
joystickAction->mButton = button;
return joystickAction;
}

struct JoystickAction* dispatchButtonActionToJoystickOne(enum Button button, u16 changed, u16 state) {
if(state & button) {
joystickOneFunPointer(createJoystickAction(button, PRESSED));
} else if(changed & button) {
joystickOneFunPointer(createJoystickAction(button, RELEASED));
}
}

void joystickHandler(u16 joystick, u16 changed, u16 state) {
if (joystick == JOY_1) {
joystickOneHandler(changed, state);
Expand All @@ -15,17 +36,10 @@ void joystickHandler(u16 joystick, u16 changed, u16 state) {
}

void joystickOneHandler(u16 changed, u16 state) {
if(state & BUTTON_LEFT) {
VDP_setHorizontalScroll(BG_B, 0);
} else if(changed & BUTTON_LEFT) {

}

if(state & BUTTON_RIGHT) {
VDP_setHorizontalScroll(BG_B, 8);
} else if(changed & BUTTON_RIGHT) {

}
dispatchButtonActionToJoystickOne(LEFT, changed, state);
dispatchButtonActionToJoystickOne(UP, changed, state);
dispatchButtonActionToJoystickOne(RIGHT, changed, state);
dispatchButtonActionToJoystickOne(DOWN, changed, state);
}

void joystickTwoHandler(u16 changed, u16 state) { }
8 changes: 4 additions & 4 deletions src/level_one.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ void loadLevelOne() {

VDP_setBackgroundColor(2);

VDP_setPalette(PAL1, tilemap.palette->data);
VDP_setPalette(PAL2, tilemap.palette->data);
VDP_loadTileSet(tilemap.tileset,1,CPU);

for(int r = 0; r < 11; r++) {
for(int c = 0; c < 12; c++) {
int index = (r * 12) + c;
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL1, TRUE, FALSE, FALSE, roomOne[index]), c + 2, r + 2);
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL2, TRUE, FALSE, FALSE, roomOne[index]), c + 2, r + 2);
}
}

for(int r = 0; r < 11; r++) {
for(int c = 0; c < 12; c++) {
int index = (r * 12) + c;
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL1, TRUE, FALSE, FALSE, roomTwo[index]), c + 2 + 15, r + 2 + 3);
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL2, TRUE, FALSE, FALSE, roomTwo[index]), c + 2 + 15, r + 2 + 3);
}
}

for(int r = 0; r < 6; r++) {
for(int c = 0; c < 3; c++) {
int index = (r * 3) + c;
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL1, TRUE, FALSE, FALSE, corridorOneToTwo[index]), c + 2 + 12, r + 2 + 4);
VDP_setTileMapXY(BG_B, TILE_ATTR_FULL(PAL2, TRUE, FALSE, FALSE, corridorOneToTwo[index]), c + 2 + 12, r + 2 + 4);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <genesis.h>

#include "level_one.h"
#include "character.h"
#include "joystick_handler.h"

#include "resource.h"

int main() {

Expand All @@ -26,8 +28,15 @@ int main() {
/* Pre-game */
loadLevelOne();

VDP_setPalette(PAL1, PlayerWalkingSprite.palette->data);

initCharacter();
setJoystickOneFunction(characterJoystick);

/* Main Loop */
while (TRUE) {
characterTick();

SPR_update();
VDP_waitVSync();
}
Expand Down

0 comments on commit 2568653

Please sign in to comment.