Skip to content

Commit

Permalink
0.19 iterated texture loading
Browse files Browse the repository at this point in the history
- added right and left walking
- loading animations in a less tedious matter using a map
  • Loading branch information
jalowe13 committed Apr 1, 2023
1 parent 0abe1d1 commit 148b2cc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ TheOneSDL/.vscode/c_cpp_properties.json
TheOneSDL/.vscode/settings.json
TheOneSDL/.vscode/tasks.json
TheOneSDL/.vscode/launch.json
*.mp4
2 changes: 1 addition & 1 deletion TheOneSDL/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Application
//Constants for reference


const char* windowTitle = "C23 Engine: The One SDL v.0.18.1 FPS:";
const char* windowTitle = "C23 Engine: The One SDL v.0.19 FPS:";

int textureWidth = 1920;
int textureHeight = 32;
Expand Down
48 changes: 29 additions & 19 deletions TheOneSDL/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Player::Player(SDL_Renderer* renderer)
tilemap_y = 16;
playerR.x = tilemap_x*32;
playerR.y = tilemap_y*32;
// Set default speed
playerSpeed = 3;
// Defaults zero no texture loaded yet
textureWidth = 0;
textureHeight = 0;
Expand All @@ -30,20 +32,24 @@ Player::Player(SDL_Renderer* renderer)
exit(-1);
}

texture = default_texture; // The current texture is the default texture
// Load Textures
// Load filenames

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

// Load Run Left
filename = run_left_file.c_str();
run_left = IMG_LoadTexture(renderer, filename);
SDL_QueryTexture(run_left, NULL, NULL, &textureWidth, &textureHeight);
std::list<std::string> tex_files = {idle, run_left, run_right};
std::list<std::string> tex_names = {"idle", "run_left","run_right"};


editMS(2);
setTexture(texture);
while (tex_files.size() > 0) {
filename = tex_files.front().c_str(); // Reference from front
std::string name = tex_names.front();
tex_names.pop_front(); // pop
tex_files.pop_front();
textures[name] = IMG_LoadTexture(renderer,filename);
std::cout << name << "loaded with " << textures[name] << std::endl;
}

editMS(3); // push default speed
setTexture(textures["idle"]);
std::cout << "Player Created!\n";

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

void Player::updateTexture(Physics* phys_eng, Terrain* terrain_eng)
{
std::cout << "[Frame]" << frame_time << std::endl;
//std::cout << "[Frame]" << frame_time << std::endl;
// << "," << getX() << "," << getY() << std::endl;

xTexEdit(getTexX() + frameWidth);
Expand All @@ -101,6 +107,7 @@ void Player::updateTexture(Physics* phys_eng, Terrain* terrain_eng)
{
handleMovement(phys_eng,terrain_eng, 1);
frame_time++;

}


Expand Down Expand Up @@ -205,10 +212,10 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng, bool animat
{
case Left:
{
std::cout << "Left\n";
// std::cout << "Left\n";
if (!inAnimation)
{
setTexture(run_left);
setTexture(textures["run_left"]);
inAnimation = true;
}
xEdit(getX() - getSpeed());
Expand All @@ -220,8 +227,11 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng, bool animat
}
case Right:
{
std::cout << "Right\n";
setTexture(texture);
if (!inAnimation)
{
setTexture(textures["run_right"]);
inAnimation = true;
}
xEdit(getX() + getSpeed());
terrain_eng->background_tilemap[tilemap_y][tilemap_x] = '~';
tilemap_x = round(getX()/32);
Expand All @@ -231,15 +241,15 @@ void Player::handleMovement(Physics* phys_eng, Terrain* terrain_eng, bool animat
}
default:
{
setTexture(texture);
setTexture(textures["idle"]);
inAnimation = false;
}
}
switch (yPath())
{
case Up:
{
std::cout << "Up\n";
// std::cout << "Up\n";
yEdit(getY() - getSpeed());
terrain_eng->background_tilemap[tilemap_y][tilemap_x] = '~';
tilemap_x = round(getX()/32);
Expand Down
19 changes: 12 additions & 7 deletions TheOneSDL/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "Terrain.h"
#include <SDL2/SDL_ttf.h>
#include <list>
#include <vector>

class Terrain;
class Physics;
Expand Down Expand Up @@ -72,6 +74,9 @@ class Player
int tilemap_x;
int tilemap_y;

//Speed
int playerSpeed;

SDL_Texture* player_texture = NULL;

MovementDirection currentDirectionX = None;
Expand All @@ -80,16 +85,16 @@ class Player


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

std::string run_left_file = "textures\\VGB\\run\\vgb_run_left-Sheet.png";
// Filenames
std::string idle = "textures\\VGB\\idle\\vgb_idle-Sheet.png";
std::string run_left = "textures\\VGB\\run\\vgb_run_left-Sheet.png";
std::string run_right = "textures\\VGB\\run\\vgb_run_right-Sheet.png";
std::list<std::string> tex_files;

//Textures stored
SDL_Texture* texture = NULL;
SDL_Texture* texture2 = NULL;
SDL_Texture* run_left = NULL;
std::map<std::string, SDL_Texture*> textures;


int frame_time;
bool inAnimation = false; // toggle to lock animation canceling
Expand Down
Binary file modified TheOneSDL/TheOne.exe
Binary file not shown.
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 148b2cc

Please sign in to comment.