Code Game
Code Game
#include<SDL.h>
#include<SDL_image.h>
#include<string>
bool init();
bool loadMedia();
void close();
//Deallocates memory
~LTexture();
#if defined(SDL_TTF_MAJOR_VERSION)
//Creates image from font string
bool loadFromRenderedText( std::string textureText, SDL_Color textColor
);
#endif
//Deallocates texture
void free();
//Set blending
void setBlendMode( SDL_BlendMode blending );
//Image dimensions
int mWidth;
int mHeight;
};
private:
//The clock time when the timer started
Uint32 mStartTicks;
private:
//The X and Y offsets of the character
int mPosX, mPosY;
//Attack animation
const int ATTACK_ANIMATION_FRAMES = 14;
SDL_Rect gSpriteClips[ ATTACK_ANIMATION_FRAMES ];
LTexture gSpriteSheetTexture;
//Scene textures
LTexture gCharacterTexture;
LTexture::LTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture()
{
//Deallocate
free();
}
//Return success
mTexture = newTexture;
return mTexture != NULL;
}
#if defined(SDL_TTF_MAJOR_VERSION)
bool LTexture::loadFromRenderedText( std::string textureText, SDL_Color textColor )
{
//Get rid of preexisting texture
free();
//Return success
return mTexture != NULL;
}
#endif
void LTexture::free()
{
//Free texture if it exists
if( mTexture != NULL )
{
SDL_DestroyTexture( mTexture );
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
//Render to screen
SDL_RenderCopyEx( gRenderer, mTexture, clip, &renderQuad, angle, center, flip
);
}
int LTexture::getWidth()
{
return mWidth;
}
int LTexture::getHeight()
{
return mHeight;
}
Character::Character()
{
//Initialize the offsets
mPosX = 0;
mPosY = 0;
//If the dot collided or went too far to the left or right
if( ( mPosX < -70 ) || ( mPosX + CHARACTER_WIDTH > SCREEN_WIDTH + 70 ) ||
checkCollision( mCollider, wall ) )
{
//Move back
mPosX -= mVelX;
mCollider.x = mPosX;
}
void Character::render()
{
//Show the dot
gCharacterTexture.render( mPosX, mPosY );
}
bool init() {
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO)<0) {
std::cout << "SDL could not initialize!" << SDL_GetError() << std::endl;
success = false;
}
else {
gWindow = SDL_CreateWindow("Game Đối Kháng", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(gWindow==NULL) {
std::cout << "Window could not be initialized!" << SDL_GetError() <<
std::endl;
success = false;
}
else {
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer==NULL) {
std::cout << "Renderer could not be initialized!" << SDL_GetError()
<< std::endl;
success = false;
}
else {
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
int imgFlags = IMG_INIT_PNG;
if(!(IMG_Init(imgFlags) & imgFlags)) {
std::cout << "SDL_image could not initialize!" <<
IMG_GetError() << std::endl;
success = false;
}
}
}
}
return success;
}
bool loadMedia() {
bool success = true;
background = loadTexture("game_material/background2.png");
if(background==NULL) {
std::cout << "Failed to load texture!" << std::endl;
success = false;
}
gSpriteClips[1].x = 192*6;
gSpriteClips[1].y = 0;
gSpriteClips[1].w = 192;
gSpriteClips[1].h = 192;
gSpriteClips[2].x = 192*7;
gSpriteClips[2].y = 0;
gSpriteClips[2].w = 192;
gSpriteClips[2].h = 192;
gSpriteClips[3].x = 0;
gSpriteClips[3].y = 192;
gSpriteClips[3].w = 192;
gSpriteClips[3].h = 192;
gSpriteClips[4].x = 192*1;
gSpriteClips[4].y = 192;
gSpriteClips[4].w = 192;
gSpriteClips[4].h = 192;
gSpriteClips[5].x = 192*2;
gSpriteClips[5].y = 192;
gSpriteClips[5].w = 192;
gSpriteClips[5].h = 192;
gSpriteClips[6].x = 192*3;
gSpriteClips[6].y = 192;
gSpriteClips[6].w = 192;
gSpriteClips[6].h = 192;
gSpriteClips[7].x = 192*4;
gSpriteClips[7].y = 192;
gSpriteClips[7].w = 192;
gSpriteClips[7].h = 192;
gSpriteClips[8].x = 192*5;
gSpriteClips[8].y = 192;
gSpriteClips[8].w = 192;
gSpriteClips[8].h = 192;
gSpriteClips[9].x = 192*6;
gSpriteClips[9].y = 192;
gSpriteClips[9].w = 192;
gSpriteClips[9].h = 192;
gSpriteClips[10].x = 192*7;
gSpriteClips[10].y = 192;
gSpriteClips[10].w = 192;
gSpriteClips[10].h = 192;
gSpriteClips[11].x = 0;
gSpriteClips[11].y = 192*2;
gSpriteClips[11].w = 192;
gSpriteClips[11].h = 192;
gSpriteClips[12].x = 192*1;
gSpriteClips[12].y = 192*2;
gSpriteClips[12].w = 192;
gSpriteClips[12].h = 192;
gSpriteClips[13].x = 192*2;
gSpriteClips[13].y = 192*2;
gSpriteClips[13].w = 192;
gSpriteClips[13].h = 192;
}
return success;
}
void close() {
gSpriteSheetTexture.free();
gCharacterTexture.free();
SDL_DestroyTexture(background);
background = NULL;
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
IMG_Quit();
SDL_Quit();
}
int frame = 0;
while(!quit) {
while(SDL_PollEvent(&e)!=0) {
if(e.type==SDL_QUIT) {
quit = true;
}
//Handle input for the character
character.handleEvent( e );
}
character.move( wall );
character.render();
SDL_RenderPresent(gRenderer);
frame++;
//Cycle animation
if( frame / 100 >= ATTACK_ANIMATION_FRAMES )
{
frame = 0;
}
}
}
}
close();
return 0;
}