Skip to content

Commit

Permalink
0.18 - Added asset swapping
Browse files Browse the repository at this point in the history
Asset swaping for key presses
  • Loading branch information
jalowe13 committed Mar 30, 2023
1 parent 0d6bafc commit 34d8313
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 11 deletions.
6 changes: 3 additions & 3 deletions TheOneSDL/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ bool Application::init()
SDL_Texture* temp_tex = NULL;

//Create Player
temp_tex = IMG_LoadTexture(renderer, "VGB_Idle.png");
player = new Player(temp_tex);
// renderer is passed in to load the texture
player = new Player(renderer);

if (!player)
{
Expand Down Expand Up @@ -176,7 +176,7 @@ void Application::update()
player->updateTexture(phys_eng, terrain_gen);
//player->handleMovement(phys_eng, terrain_gen);
terrain_gen->fillScreen();
// system("pause"); // Frame by frame
//system("pause"); // Frame by frame
}

void Application::render()
Expand Down
2 changes: 1 addition & 1 deletion TheOneSDL/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void Physics::resetTime(){
}

float Physics::getTime(){
std::cout << "Time:" << time << std::endl;
// std::cout << "Time:" << time << std::endl;
return time;
}

Expand Down
43 changes: 38 additions & 5 deletions TheOneSDL/Player.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
#include "Player.h"
#include <cmath>

Player::Player(SDL_Texture* default_texture)
Player::Player(SDL_Renderer* renderer)
{
playerR.h = 32;
playerR.w = 32;
tilemap_x = 1;
tilemap_y = 16;
playerR.x = tilemap_x*32;
playerR.y = tilemap_y*32;
textureWidth = 1920;
textureHeight = 32;
// Defaults zero no texture loaded yet
textureWidth = 0;
textureHeight = 0;
frameWidth = textureWidth / 60;
frameHeight = textureHeight;
frame_time = 0;

// Check default file existance and load default texture
std::string idle_file = "textures\\VGB\\idle\\vgb_idle-Sheet.png";
const char* filename = idle_file.c_str();
SDL_Texture* default_texture = IMG_LoadTexture(renderer, filename);
SDL_QueryTexture(default_texture, NULL, NULL, &textureWidth, &textureHeight);

// Load check
if ( textureWidth == 0 || textureHeight == 0)
{
std::cout << "Error Player.cpp: Texture not loaded " << filename << "with dims " <<
textureWidth << " and " << textureHeight << std::endl;
exit(-1);
}

texture = default_texture; // The current texture is the default texture

// Test 2
filename = idle_file2.c_str();
texture2 = IMG_LoadTexture(renderer, filename);
SDL_QueryTexture(default_texture, NULL, NULL, &textureWidth, &textureHeight);

editMS(2);
setTexture(default_texture);
setTexture(texture);
std::cout << "Player Created!\n";

}
Expand Down Expand Up @@ -57,7 +80,7 @@ SDL_Texture* Player::getTexture()

void Player::updateTexture(Physics* phys_eng, Terrain* terrain_eng)
{
//std::cout << "[Type,F,TEX_X,X,Y]" << texture_name << "," << frame_time << "," << getTexX()
std::cout << "[Type,F,TEX_X,X,Y]" << idle_file << "," << frame_time << "," << getTexX() << std::endl;
// << "," << getX() << "," << getY() << std::endl;

xTexEdit(getTexX() + frameWidth);
Expand All @@ -74,6 +97,8 @@ void Player::updateTexture(Physics* phys_eng, Terrain* terrain_eng)
handleMovement(phys_eng,terrain_eng, 1);
frame_time++;
}


}

int Player::getY()
Expand Down Expand Up @@ -109,11 +134,13 @@ void Player::yPathEdit(MovementDirection path)
void Player::xEdit(int x)
{
playerR.x = x;
setTexture(texture2);
}

void Player::yEdit(int y)
{
playerR.y = y;

}

void Player::wEdit(int w)
Expand All @@ -129,6 +156,7 @@ void Player::hEdit(int h)
void Player::xTexEdit(int x)
{
textureR.x = x;

}

void Player::yTexEdit(int y)
Expand Down Expand Up @@ -194,13 +222,18 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng, bool animat
{
case Up:
{
setTexture(texture2);
yEdit(getY() - getSpeed());
terrain_eng->background_tilemap[tilemap_y][tilemap_x] = '~';
tilemap_x = round(getX()/32);
tilemap_y = round(getY()/32);
terrain_eng->background_tilemap[tilemap_y][tilemap_x] = '1';
break;
}
case None:
{
setTexture(texture);
}
// Case Down will be needed in future not currently
// If Case down is needed implement collision check
// case Down:
Expand Down
10 changes: 8 additions & 2 deletions TheOneSDL/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum MovementDirection {
class Player
{
public:
Player(SDL_Texture* default_texture);
Player(SDL_Renderer* default_texture);
~Player();

void updateTexture(Physics* phys_eng, Terrain* terrain_eng);
Expand Down Expand Up @@ -80,7 +80,13 @@ class Player


int textureWidth, textureHeight, frameWidth, frameHeight;
std::string texture_name = "VGB_Idle";
//std::string texture_name = "VGB_Idle";
std::string idle_file = "textures\\VGB\\idle\\vgb_idle-Sheet.png";
std::string idle_file2 = "VGB_Idle.png";

//Textures stored
SDL_Texture* texture = NULL;
SDL_Texture* texture2 = NULL;

int frame_time;

Expand Down
Binary file modified TheOneSDL/TheOne.exe
Binary file not shown.
Binary file added TheOneSDL/textures/VGB/idle/vgb_idle-Sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added TheOneSDL/vgb_idle2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 34d8313

Please sign in to comment.