It's been awhile since I've needed help, but I can't for the life of me figure out why subtraction isn't doing what it should be I'm clearly missing something or some change to how C++ interprets subtraction, if you could point me in the right direction that would be awesome otherwise I'll just do a complete rewrite.
(Also ignore int sx, int sy I was using those to keep track of the position but using mOffset was simply easier)
//main
#include "gfx.hxx"
#include "regfx.hxx"
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
gfx g(false,1280,768,"Sprite Runner");
bool quit = false;
SDL_Event e;
Sprite Menu("Menu.bmp",ren);
ofstream file;
file.open("sp.log");
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
quit = true;
}
if(e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch(e.key.keysym.sym)
{
case SDLK_w:
Menu.mOffset.y = Menu.mOffset.y - 1;
case SDLK_s:
Menu.mOffset.y += 1;
case SDLK_a:
Menu.mOffset.x = Menu.mOffset.x -1;
case SDLK_d:
Menu.mOffset.x += 1;
}
}
//Menu.mOffset.x = Menu.sx;
//Menu.mOffset.y = Menu.sy;
//file << "sx: " << Menu.sx << "\t" << "sy: " << Menu.sy << endl;
//file << "mOffset.x: " << Menu.mOffset.x << "\t" << Menu.mOffset.y << endl;
}
SDL_RenderClear(ren);
Menu.render(ren);
SDL_RenderPresent(ren);
}
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(g.getWindow());
file.close();
SDL_Quit();
return 0;
}
//regfx aka sprite class
#ifndef _REGFX_HXX_
#define _REGFX_HXX_
#include <iostream>
#include <string>
#include <fstream>
#include <SDL.h>
class Sprite
{
private:
SDL_Texture * mSprite;
int mWidth;
int mHeight;
public:
SDL_Rect mOffset;
int sx; //xloc
int sy; //yloc
Sprite(std::string imagePath, SDL_Renderer * rnd)
{
SDL_Surface * spriteRes = SDL_LoadBMP(imagePath.c_str());
mWidth = spriteRes->w;
mHeight = spriteRes->h;
mSprite = SDL_CreateTextureFromSurface(rnd,spriteRes);
sx = 0;
sy = 0;
mOffset = {sx,sy,mWidth,mHeight};
}
~Sprite(){}
void setX(int x){sx = sx + x; mOffset.x = sx;}
void setY(int y){sy = sy + y; mOffset.y = sy;}
int getX(){return sx;}
int getY(){return sy;}
void render(SDL_Renderer * rnd)
{
SDL_RenderCopy(rnd,mSprite,NULL,&mOffset);
}
};
#endif // _REGFX_HXX_
I believe that there may be some weird stuff going on in the background, going off of past experience x -= 1 is the same as x = (x-1), I've written far more complicated pieces of code but i'm having some trouble narrowing this issue down I've tried multiple approaches to get it to function as intended but no matter what i try either only down(s) and right(d) work or none do.