Texture implementation
This commit is contained in:
parent
a0ed188138
commit
fc35d1c9a5
|
@ -1,2 +1,4 @@
|
|||
*.swp
|
||||
*.o
|
||||
*.swo
|
||||
*.swn
|
||||
|
|
15
Makefile
15
Makefile
|
@ -1,24 +1,24 @@
|
|||
#-*- Makefile -*-
|
||||
|
||||
OBJS = main.o player.o dt.o block.o entity.o camera.o
|
||||
OBJS = main.o player.o dt.o block.o entity.o camera.o texture.o
|
||||
|
||||
CC = g++
|
||||
|
||||
COMPILER_FLAGS = -w
|
||||
|
||||
LINKER_FLAGS = -lSDL2 -std=gnu++11 -lSDL2_image
|
||||
LINKER_FLAGS = -lSDL2_image -lSDL2 -std=gnu++11
|
||||
|
||||
OBJ_NAME = bin/main
|
||||
OBJ_NAME = main
|
||||
|
||||
|
||||
all: $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPLER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
|
||||
main.o: main.cpp
|
||||
main.o: main.cpp texture.o
|
||||
$(CC) -c main.cpp $(LINKER_FLAGS)
|
||||
|
||||
player.o: player.cpp player.h entity.o
|
||||
$(CC) -c player.cpp -lm $(LINKER_FLAGS)
|
||||
player.o: player.cpp player.h entity.o texture.o
|
||||
$(CC) -c player.cpp -lm $(LINKER_FLAGS)
|
||||
|
||||
dt.o: dt.cpp dt.h
|
||||
$(CC) -c dt.cpp $(LINKER_FLAGS)
|
||||
|
@ -31,3 +31,6 @@ entity.o: entity.cpp entity.h
|
|||
|
||||
camera.o: camera.cpp camera.h
|
||||
$(CC) -c camera.cpp
|
||||
|
||||
texture.o: texture.cpp texture.h
|
||||
$(CC) -c -lSDL2_image texture.cpp
|
||||
|
|
8
main.cpp
8
main.cpp
|
@ -2,7 +2,6 @@
|
|||
|
||||
#include<iostream>
|
||||
#include<SDL2/SDL.h>
|
||||
//#include<SDL2/SDL_image.h>
|
||||
#include"player.h"
|
||||
#include"block.h"
|
||||
#include"camera.h"
|
||||
|
@ -28,7 +27,6 @@ bool loadMedia(){
|
|||
|
||||
void init(){
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
//IMG_Init(IMG_INIT_JPG);
|
||||
gWindow = SDL_CreateWindow("Platform Test!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
|
||||
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);
|
||||
|
@ -40,7 +38,6 @@ void close(){
|
|||
gWindow = NULL;
|
||||
gRenderer = NULL;
|
||||
|
||||
//IMG_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
|
@ -68,13 +65,12 @@ int main(int argc, char* args[]){
|
|||
quit = true;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(gRenderer,0,0,0,0xFF);
|
||||
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
|
||||
SDL_RenderClear(gRenderer);
|
||||
|
||||
|
||||
camera.update(posweg.getRectangle().x,posweg.getRectangle().y);
|
||||
|
||||
posweg.print(camera.getPosX());
|
||||
std::cout << posweg.getRectangle().x << std::endl;
|
||||
|
||||
wallA.print(camera.getPosX());
|
||||
wallB.print(camera.getPosX());
|
||||
|
|
48
player.cpp
48
player.cpp
|
@ -6,9 +6,12 @@ void Player::print(int cameraX){
|
|||
|
||||
SDL_Rect cameraFix = rect;
|
||||
cameraFix.x -= cameraX;
|
||||
|
||||
|
||||
SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF);
|
||||
SDL_RenderFillRect(*renderer, &cameraFix);
|
||||
ply.render(&cameraFix,currentFrame,renderer);
|
||||
if(ifRunning)ply.render(&cameraFix,&plyFrame[5],renderer);
|
||||
|
||||
currentFrame = &plyFrame[0];
|
||||
|
||||
oldPosX = posX;
|
||||
oldPosY = posY;
|
||||
|
@ -25,7 +28,35 @@ void Player::init(int x,int y, int w, int h, SDL_Renderer** render){
|
|||
renderer = render;
|
||||
|
||||
oldPosX = posX;
|
||||
oldPosY = posY; };
|
||||
oldPosY = posY;
|
||||
|
||||
loadMedia();
|
||||
|
||||
currentFrame = &plyFrame[0];
|
||||
};
|
||||
|
||||
bool Player::loadMedia(){
|
||||
bool success = true;
|
||||
|
||||
if(!ply.loadTexture("textures/player.png",renderer)){
|
||||
std::cout << "Failed to load ply texture" << std::endl;
|
||||
success = false;
|
||||
}
|
||||
else{
|
||||
int frame = 0;
|
||||
for(int row = 0;row < 2;row++){
|
||||
for(int column = 0;column < 3;column++){
|
||||
plyFrame[frame].w=szW;
|
||||
plyFrame[frame].h=szH;
|
||||
plyFrame[frame].x=szW*column;
|
||||
plyFrame[frame].y=szH*row;
|
||||
frame++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
int Player::intVelX(){
|
||||
return static_cast<int>(velocityX);
|
||||
|
@ -42,6 +73,7 @@ void Player::move(){
|
|||
int dur = 4; //Divide transition time
|
||||
int speed = 200; //Horizontal movement speed
|
||||
float run = 1.5; //Running speed multiplication
|
||||
ifRunning = false;
|
||||
//static bool isRunning = false;
|
||||
|
||||
int gravity = 800; //Gravity force
|
||||
|
@ -50,7 +82,9 @@ void Player::move(){
|
|||
//Check keyboard current state
|
||||
if(currentKeyStates[SDL_SCANCODE_LEFT]) direction += -1;
|
||||
if(currentKeyStates[SDL_SCANCODE_RIGHT]) direction += 1;
|
||||
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning) run = 1;
|
||||
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning){
|
||||
run = 1;
|
||||
}
|
||||
if(run!=1) dur /= 1;
|
||||
|
||||
//Set velocity
|
||||
|
@ -87,6 +121,8 @@ void Player::move(){
|
|||
if(isRunning == false) run = 1;
|
||||
}
|
||||
|
||||
if(run!=1) ifRunning = true;
|
||||
|
||||
//Get the position and update the velY with gravity
|
||||
velocityY -= gravity * dt;
|
||||
float x = velocityX * dt * run;
|
||||
|
@ -163,11 +199,13 @@ int Player::check(SDL_Rect rectA){
|
|||
ground = true;
|
||||
collision = 2;
|
||||
velocityY = 0;
|
||||
currentFrame = &plyFrame[1];
|
||||
}
|
||||
//Bottom collision
|
||||
else if(posY == b2Y){
|
||||
collision = 3;
|
||||
topCollision = true;
|
||||
currentFrame = &plyFrame[3];
|
||||
if(velocityY > 0)
|
||||
velocityY -= static_cast<int>(velocityY);
|
||||
}
|
||||
|
@ -178,12 +216,14 @@ int Player::check(SDL_Rect rectA){
|
|||
//Left collision
|
||||
if(posX + szW == bX){
|
||||
collision = 4;
|
||||
currentFrame = &plyFrame[2];
|
||||
if(velocityX > 0)
|
||||
velocityX -= static_cast<int>(velocityX);
|
||||
}
|
||||
//Right collision
|
||||
else if(posX == b2X){
|
||||
collision = 5;
|
||||
currentFrame = &plyFrame[4];
|
||||
if(velocityX < 0)
|
||||
velocityX -= static_cast<int>(velocityX);
|
||||
}
|
||||
|
|
11
player.h
11
player.h
|
@ -5,9 +5,10 @@
|
|||
|
||||
#include<SDL2/SDL.h>
|
||||
#include<iostream>
|
||||
#include<cmath>
|
||||
#include"dt.h"
|
||||
#include"entity.h"
|
||||
#include<cmath>
|
||||
#include"texture.h"
|
||||
|
||||
class Entity;
|
||||
|
||||
|
@ -16,9 +17,9 @@ class Player: public Entity{
|
|||
void print(int cameraX);
|
||||
int check(SDL_Rect rectB);
|
||||
void init(int x, int y,int w, int h,SDL_Renderer** render);
|
||||
//SDL_Rect getRectangle();
|
||||
private:
|
||||
int* coll;
|
||||
bool loadMedia();
|
||||
void move();
|
||||
int intVelX();
|
||||
SDL_Renderer** renderer;
|
||||
|
@ -29,7 +30,13 @@ class Player: public Entity{
|
|||
float velocityX = 0;
|
||||
float velocityY = 0;
|
||||
bool isRunning = false;
|
||||
bool ifRunning = false;
|
||||
int oldPosX, oldPosY;
|
||||
|
||||
PosuTexture ply;
|
||||
SDL_Rect* currentFrame;
|
||||
const static int plyNum = 6;
|
||||
SDL_Rect plyFrame[plyNum];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
#include"texture.h"
|
||||
|
||||
PosuTexture::PosuTexture(){
|
||||
//Initialize variables
|
||||
texture = NULL;
|
||||
szW = 0;
|
||||
szH = 0;
|
||||
|
||||
IMG_Init(IMG_INIT_JPG);
|
||||
//Set renderer
|
||||
}
|
||||
|
||||
PosuTexture::~PosuTexture(){
|
||||
//Deallocate
|
||||
free();
|
||||
}
|
||||
|
||||
bool PosuTexture::loadTexture(std::string path,SDL_Renderer** renderer){
|
||||
//Get rid of preexisting texture
|
||||
free();
|
||||
|
||||
//Load image at specified path
|
||||
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
|
||||
|
||||
if(loadedSurface == NULL)
|
||||
std::cout << "Couldn't load " << path.c_str() << std::endl;
|
||||
else{
|
||||
//Create texture from surface pixels
|
||||
texture = SDL_CreateTextureFromSurface
|
||||
(*renderer,loadedSurface);
|
||||
|
||||
if(texture == NULL){
|
||||
std::cout << "Couldn't to create texture from "
|
||||
<< path.c_str() << std::endl;
|
||||
}
|
||||
else{
|
||||
//Get image dimensions
|
||||
szW = loadedSurface->w;
|
||||
szH = loadedSurface->h;
|
||||
}
|
||||
//Get rid of old loaded surface
|
||||
SDL_FreeSurface(loadedSurface);
|
||||
}
|
||||
|
||||
return texture != NULL;
|
||||
}
|
||||
|
||||
void PosuTexture::free(){
|
||||
//Free texture if it exists
|
||||
if(texture != NULL){
|
||||
SDL_DestroyTexture(texture);
|
||||
texture = NULL;
|
||||
szW = 0;
|
||||
szH = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void PosuTexture::render(SDL_Rect* quad,SDL_Rect* frame,SDL_Renderer** renderer){
|
||||
//Set clip rendering dimensions
|
||||
/*if(frame != NULL){
|
||||
quad.w = frame->w;
|
||||
quad.h = frame->h;
|
||||
}*/
|
||||
|
||||
//Render to screen
|
||||
SDL_RenderCopy(*renderer,texture,frame,quad);
|
||||
}
|
||||
|
||||
int PosuTexture::getWidth(){
|
||||
return szW;
|
||||
}
|
||||
|
||||
int PosuTexture::getHeight(){
|
||||
return szH;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
//Texture management class header
|
||||
|
||||
#ifndef __TEXTURE_H_INCLUDED__
|
||||
#define __TEXTURE_H_INCLUDED__
|
||||
|
||||
#include<SDL2/SDL.h>
|
||||
#include<SDL2/SDL_image.h>
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
|
||||
class PosuTexture{
|
||||
public:
|
||||
PosuTexture();
|
||||
~PosuTexture();
|
||||
bool loadTexture(std::string path,SDL_Renderer** renderer);
|
||||
void free();
|
||||
void render(SDL_Rect* quad,SDL_Rect* frame,SDL_Renderer** renderer);
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
private:
|
||||
SDL_Texture* texture;
|
||||
int szW, szH;
|
||||
};
|
||||
|
||||
#endif
|
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
Loading…
Reference in New Issue