I'm making a project in C++ SFML. Below you can see a small fragment of the animation "engine" code. This is my problem: in the movment funcion, depending on what key is presed the direction variable "dir" changes. Then, in int main(), the aniamte_char function takes a sprite as one of it's parameters, in this case, one element of the "ch[]" sprite array that has the id "e.dir". Now what happens, is that "e.dir=0" works fine, the animation plays normaly, But for "e.dir=1,2,3,4" nothing displays. I tried fiddling aroud to fix this problem but nothing seems to work and I'm clueless;/ If somebody knows why this is happening please help.
int_main()
#include <SFML/Graphics.hpp>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include "InfiPower.h"
#define WIDTH 1200
#define HEIGHT 800
#define FRQ 10
using namespace std;
using namespace sf;
int main()
{
RenderWindow win(VideoMode(WIDTH,HEIGHT),"takie cos lol");
win.setFramerateLimit(60);
InfiPower e(WIDTH/2-25,HEIGHT/2-25,true,0,0,-1);
Texture bg_[1];
bg_[0].loadFromFile("Textures/bg.png");
Texture ch_[5];
ch_[0].loadFromFile("Textures/char_idle.png");
ch_[1].loadFromFile("Textures/char_right.png");
ch_[2].loadFromFile("Textures/char_up.png");
ch_[3].loadFromFile("Textures/char_left.png");
ch_[4].loadFromFile("Textures/char_down.png");
Sprite ch[5];
ch[0].setTexture(ch_[0]);
ch[1].setTexture(ch_[1]);
ch[2].setTexture(ch_[2]);
ch[3].setTexture(ch_[3]);
ch[4].setTexture(ch_[4]);
e.cur_bg=0;
while(win.isOpen())
{
win.clear();
e.movement(10,10000,WIDTH);
e.set_background(win,bg_);
cout<<e.dir<<endl;
e.animate_char(win,ch[e.dir],100,FRQ);
if(Keyboard::isKeyPressed(Keyboard::Escape))
{
exit(0);
}
win.display();
}
}
fragment of my animation "engine":
#include <SFML/Graphics.hpp>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include "InfiPower.h"
using namespace std;
using namespace sf;
void InfiPower::animate_char(RenderWindow & win,Sprite & s,int cell_width,int caf)
{
s.setPosition(c_xo,c_yo);
s.setTextureRect(IntRect(state*cell_width,dir*cell_width,cell_width,cell_width));
if(anim_c==caf)
{
state++;
anim_c=0;
}
if(state>=4)
{
state=0;
}
win.draw(s);
anim_c++;
}
void InfiPower::movement(int ms,int b,int w)
{
if(Keyboard::isKeyPressed(Keyboard::D))
{
if(bg_x>=((b-w)*(-1)+ms))
{
in_motion=true;
bg_x=bg_x-ms;
}
last_dir=1;
dir=1;
}
else if(Keyboard::isKeyPressed(Keyboard::A))
{
if(bg_x+ms<=0)
{
in_motion=true;
bg_x=bg_x+ms;
}
last_dir=3;
dir=3;
}
else if(Keyboard::isKeyPressed(Keyboard::S))
{
if(if_4_dirs==true)
{
//if(bg_y-ms>=800)
//{
in_motion=true;
bg_y-=ms;
//}
last_dir=4;
dir=4;
}
}
else if(Keyboard::isKeyPressed(Keyboard::W))
{
if(if_4_dirs==true)
{
//if(bg_y+ms<=0)
//{
in_motion=true;
bg_y+=ms;
//}
last_dir=2;
dir=2;
}
}
else
{
in_motion=false;
dir=0;
}
}
void InfiPower::set_background(RenderWindow & win,Texture bg_[])
{
Sprite bg;
bg.setPosition(bg_x,bg_y);
for(int i=0; i>=(-1); i++)
{
if(i==cur_bg)
{
bg.setTexture(bg_[i]);
win.draw(bg);
break;
}
}
}
My guess is this line is setting your Y coordinate source rectangle to the next row down. For dir=0, this is the top row and would work correctly:
s.setTextureRect(IntRect(state*cell_width,dir*cell_width,cell_width,cell_width));
If there is one row, then it should be:
s.setTextureRect(IntRect(state*cell_width,0,cell_width,cell_width));
I want to say something and I'm not trying to be mean or disrespectful, but I think you should slow down and learn a little more coding before proceeding. You have bitten off more than you can chew with this current project. (Based on this and previous questions)
My concern is that you are doing a lot of 'bad' things that will slow you down until you learn to fix them. E.g. for(int i=0; i>=(-1); i++)
means while(true) until integer overflow
. Just because the code "works" doesn't mean that it is what you mean to say. Semantics matter a great deal in programming.
As state above, you should learn how to use a debugger and step through code. This is a very fundamental skill.
I would suggest taking a class on Programming, reading a few books, studying other people's code, etc...
I also HIGHLY recommend learning game engine programming by using a commercial engine and learning how they do this. This is a TOP-DOWN approach and lets you learn the more abstract concepts before tackling low level stuff. I recommend something like Unity.