player controller + renderer
This commit is contained in:
parent
4913fc7667
commit
a1f8e26d9a
21 changed files with 348 additions and 279 deletions
|
@ -106,7 +106,6 @@ void runMapEditor() {
|
|||
|
||||
done = true;
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplSDLRenderer_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
|
|
|
@ -13,6 +13,10 @@ GlobalState state;
|
|||
void PRunBoostrap() {
|
||||
PLoadTextureAsset("assets/textures/wall_brick.png");
|
||||
PLoadTextureAsset("assets/textures/floor.png");
|
||||
PLoadTextureAsset("assets/player/player_walk_down.png");
|
||||
PLoadTextureAsset("assets/player/player_walk_up.png");
|
||||
PLoadTextureAsset("assets/player/player_walk_left.png");
|
||||
PLoadTextureAsset("assets/player/player_walk_right.png");
|
||||
}
|
||||
|
||||
PhoenixGame::PhoenixGame() {
|
||||
|
@ -46,6 +50,7 @@ void PhoenixGame::render() {
|
|||
SDL_RenderClear(state.renderer);
|
||||
|
||||
PRenderMap(&m_currentMap);
|
||||
PRenderPlayer(&state.player);
|
||||
|
||||
// PRenderTexture(&m_texture, 0, 0);
|
||||
}
|
||||
|
@ -64,26 +69,25 @@ void PhoenixGame::run() {
|
|||
}
|
||||
|
||||
unsigned char const *keys = SDL_GetKeyboardState(nullptr);
|
||||
if (keys[SDL_SCANCODE_UP]) {
|
||||
m_viewport.w += 5;
|
||||
m_viewport.h += 5;
|
||||
} else if (keys[SDL_SCANCODE_DOWN]) {
|
||||
m_viewport.w -= 5;
|
||||
m_viewport.h -= 5;
|
||||
} else if (keys[SDL_SCANCODE_W]) {
|
||||
m_viewport.y += 20;
|
||||
if (m_viewport.y > 0)
|
||||
m_viewport.y = 0;
|
||||
} else if (keys[SDL_SCANCODE_S]) {
|
||||
m_viewport.y -= 20;
|
||||
if (m_viewport.y < -720)
|
||||
m_viewport.y = -720;
|
||||
} else if (keys[SDL_SCANCODE_A]) {
|
||||
m_viewport.x += 20;
|
||||
} else if (keys[SDL_SCANCODE_D]) {
|
||||
m_viewport.x -= 20;
|
||||
if (m_viewport.x < -1280)
|
||||
m_viewport.x = -1280;
|
||||
|
||||
if (keys[SDL_SCANCODE_W]) {
|
||||
state.player.facing = Up;
|
||||
state.player.y -= 5;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_S]) {
|
||||
state.player.facing = Down;
|
||||
state.player.y += 5;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_A]) {
|
||||
state.player.facing = Left;
|
||||
state.player.x -= 5;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_D]) {
|
||||
state.player.facing = Right;
|
||||
state.player.x += 5;
|
||||
}
|
||||
|
||||
// Rendering code
|
||||
|
|
29
src/player.cpp
Normal file
29
src/player.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "player.hpp"
|
||||
#include "assets.hpp"
|
||||
#include "config.hpp"
|
||||
#include "state.hpp"
|
||||
#include "texture.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
void PRenderPlayer(PPlayer *player) {
|
||||
PhoenixTexture *playerUpTexture = PGetTextureById(PLAYER_WALK_UP_TEXTURE_ID);
|
||||
PhoenixTexture *playerDownTexture =
|
||||
PGetTextureById(PLAYER_WALK_DOWN_TEXTURE_ID);
|
||||
PhoenixTexture *playerLeftTexture =
|
||||
PGetTextureById(PLAYER_WALK_LEFT_TEXTURE_ID);
|
||||
PhoenixTexture *playerRightTexture =
|
||||
PGetTextureById(PLAYER_WALK_RIGHT_TEXTURE_ID);
|
||||
|
||||
if (player->facing == Up) {
|
||||
PRenderTexture(playerUpTexture, player->x, player->y);
|
||||
} else if (player->facing == Down) {
|
||||
PRenderTexture(playerDownTexture, player->x, player->y);
|
||||
} else if (player->facing == Left) {
|
||||
PRenderTexture(playerLeftTexture, player->x, player->y);
|
||||
} else if (player->facing == Right) {
|
||||
PRenderTexture(playerRightTexture, player->x, player->y);
|
||||
} else {
|
||||
throw std::runtime_error(
|
||||
"Invalid player->facing direction, expected Up, Down, Left, or Right");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue