[ad_1]
I’m following together with the LazyFoo tutorial to create the idea for a easy arcade area shooter. I’m attempting to get the sprite transferring by way of the map whereas being tracked by the digital camera. The code I’ve works within the x course however would not scroll/observe within the y course. I am unable to determine why as a result of the code for each instructions is similar.
Here’s what I’ve, the digital camera operate is on the backside (line 300ish) in the principle operate.
#embody <iostream>
#embody <stdio.h>
#embody <SDL2/SDL.h>
#embody <sstream>
#embody <string>
#embody <SDL2/SDL_image.h>
SDL_DisplayMode present;
const int LEVEL_WIDTH = 1280;
const int LEVEL_HEIGHT = 960;
int SCREEN_WIDTH = 1000;
int SCREEN_HEIGHT = 1000;
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Texture* ship = NULL;
SDL_Renderer* gRenderer = NULL;
//Texture wrapper class
class LTexture
{
public:
//Initializes variables
LTexture();
//Deallocates reminiscence
~LTexture();
//Loads picture at specified path
bool loadFromFile( std::string path );
//Deallocates texture
void free();
//Renders texture at given level
void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* heart = NULL, SDL_RendererFlip = SDL_FLIP_NONE );
//Gets picture dimensions
int getWidth();
int getHeight();
void setWidth( int width );
void setHeight( int top );
non-public:
//The precise {hardware} texture
SDL_Texture* mTexture;
//Image dimensions
int mWidth;
int mHeight;
};
LTexture::LTexture() {
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture() {
free();
}
bool LTexture::loadFromFile( std::string path ) {
free();
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
// SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface);
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
SDL_FreeSurface( loadedSurface );
mTexture = newTexture;
return mTexture != NULL;
}
void LTexture::free() {
if (mTexture != NULL) {
SDL_DestroyTexture( mTexture );
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void LTexture::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* heart, SDL_RendererFlip flip ) {
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
if ( clip != NULL ) {
renderQuad.h = clip->h;
renderQuad.w = clip->w;
}
SDL_RenderCopyEx( gRenderer, mTexture, clip, &renderQuad, angle, heart, flip );
}
int LTexture::getWidth() {
return mWidth;
}
int LTexture::getHeight() {
return mHeight;
}
void LTexture::setWidth( int width ) {
mWidth = width;
}
void LTexture::setHeight( int top ) {
mHeight = top;
}
class Ship {
public:
// Ship dimensions
const static int SHIP_HEIGHT = 100;
const static int SHIP_WIDTH = 100;
// Ship most velocity
static const int SHIP_VELOCITY = 10;
Ship();
void handleEvent( SDL_Event &e );
int getVelY();
int getVelX();
int getPosX();
int getPosY();
void transfer();
void render( int camX, int camY );
non-public:
int mPosX, mPosY;
int mVelX, mVelY;
};
LTexture shipTexture;
LTexture backgroundTexture;
Ship::Ship() {
mPosX = 0;
mPosY = 0;
mVelX = 0;
mVelY = 0;
}
int Ship::getVelY() {
return mVelY;
}
int Ship::getVelX() {
return mVelX;
}
void Ship::handleEvent( SDL_Event &e ) {
if ( e.sort == SDL_KEYDOWN && e.key.repeat == 0 ) {
change (e.key.keysym.sym) {
case SDLK_UP: mVelY -= SHIP_VELOCITY; break;
case SDLK_DOWN: mVelY += SHIP_VELOCITY; break;
case SDLK_LEFT: mVelX -= SHIP_VELOCITY; break;
case SDLK_RIGHT: mVelX += SHIP_VELOCITY; break;
}
}
else if ( e.sort == SDL_KEYUP && e.key.repeat == 0 ) {
change (e.key.keysym.sym) {
case SDLK_UP: mVelY += SHIP_VELOCITY; break;
case SDLK_DOWN: mVelY -= SHIP_VELOCITY; break;
case SDLK_LEFT: mVelX += SHIP_VELOCITY; break;
case SDLK_RIGHT: mVelX -= SHIP_VELOCITY; break;
}
}
}
void Ship::transfer() {
mPosX += mVelX;
if ( mPosX < 0 || mPosX + SHIP_WIDTH > LEVEL_WIDTH ) {
mPosX -= mVelX;
}
mPosY += mVelY;
if ( mPosY < 0 || mPosY + SHIP_HEIGHT > LEVEL_HEIGHT ) {
mPosY -= mVelY;
}
}
void Ship::render( int camX, int camY ) {
double angle;
if ( mVelX > 0 )
angle = 90.0;
if ( mVelX < 0 )
angle = 270.0;
if ( mVelY > 0 )
angle = 180.0;
if ( mVelY < 0 )
angle = 0.0;
shipTexture.render( mPosX - camX, mPosY - camY, NULL, angle, NULL );
}
int Ship::getPosX() {
return mPosX;
}
int Ship::getPosY() {
return mPosY;
}
void init() SDL_RENDERER_PRESENTVSYNC );
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
// Enable PNG loading
int imgFlags = IMG_INIT_PNG;
IMG_Init( imgFlags );
// Format display screen dimension
SDL_DisplayMode present;
SDL_GetCurrentDisplayMode(0, &present );
SCREEN_WIDTH = present.w;
SCREEN_HEIGHT = present.h;
SDL_Log("Display #%d: present show mode is %dxpercentdpx @ %dhz.", 0, present.w, present.h, present.refresh_rate);
void loadMedia() {
shipTexture.loadFromFile( "8.png" );
backgroundTexture.loadFromFile( "509906.png" );
backgroundTexture.setWidth( SCREEN_WIDTH );
backgroundTexture.setHeight( SCREEN_HEIGHT );
}
void shut() {
shipTexture.free();
SDL_DestroyWindow( gWindow );
gWindow = NULL;
SDL_DestroyRenderer( gRenderer );
gRenderer = NULL;
SDL_Quit();
IMG_Quit();
}
int most important( int argc, char* args[] ) {
bool give up = false;
SDL_Event e;
Ship ship;
SDL_Rect digital camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
init();
loadMedia();
whereas( !give up ) {
whereas ( SDL_PollEvent( &e ) !=0 ) {
if( e.sort == SDL_QUIT ) {
give up = true;
}
ship.handleEvent( e );
}
ship.transfer();
digital camera.x = ( ship.getPosX() + Ship::SHIP_WIDTH / 2 ) - SCREEN_WIDTH / 2;
digital camera.y = ( ship.getPosY() + Ship::SHIP_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;
if ( digital camera.x < 0 )
digital camera.x = 0;
if ( digital camera.y < 0 )
digital camera.y = 0;
if ( digital camera.x > LEVEL_WIDTH - digital camera.w )
digital camera.x = LEVEL_WIDTH - digital camera.w;
if ( digital camera.y > LEVEL_HEIGHT - digital camera.h )
digital camera.y = LEVEL_HEIGHT - digital camera.h;
// Clear the display screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
backgroundTexture.render( 0, 0, &digital camera );
ship.render( digital camera.x, digital camera.y );
// Update display screen
SDL_RenderPresent( gRenderer );
}
shut();
return 0;
}
```
[ad_2]