I installed SFML 2.0 Wednesday, and I've been tinkering around with it trying to make a basic shooter game.
After I press Start on the title screen, instead of the game screen, a scaled-to-screen and upside down version of the ship sprite is rendered. The weird this is, when I put the game in fullscreen and then back to windowed mode, it displays as it should. Even weirder? The huge sprite is rendered even I comment out the line to draw it in the game loop. o.O
I used to use Allegro and have a very specific way of coding that's probably not really intuitive to anyone but me. It makes me unsure of what code to post, but I'll try.
Graphic structure:
#include "Graphic.h"
//For future reference, I typdef-d sf::Image as Bitmap and sf::Texture as Image if anything looks confusing.
Graphic::Graphic(string file) {
img.LoadFromFile(file);
img.SetSmooth(false);
spr.SetTexture(img);
x = spr.GetPosition().x;
y = spr.GetPosition().y;
w = spr.GetSize().x;
h = spr.GetSize().y;
n = spr.GetRotation();
}
Graphic::Graphic(short w, short h) {
img.Create(w, h);
img.SetSmooth(false);
spr.SetTexture(img);
x = spr.GetPosition().x;
y = spr.GetPosition().y;
w = spr.GetSize().x;
h = spr.GetSize().y;
n = spr.GetRotation();
}
void Graphic::Copy(Canvas &bb) {
img = bb.buff.GetTexture();
spr.SetTexture(img);
Bitmap bmp = img.CopyToImage();
bmp.SaveToFile("filer.png");
}
void Graphic::Blit(Canvas &buff = *Buffer) {
buff.Draw(*this);
}
//Irrelevant code...
Canvas/Buffer structure:
Canvas *Buffer;
Canvas::Canvas(short ww, short hh) : w(ww), h(hh), x(0), y(0) {
buff.Create(w, h);
buff.Clear(CLEAR); //sf::Color(255, 0 , 255)
spr.SetTexture(buff.GetTexture());
}
//Irrelevant code...
void Canvas::Tint(Color cc){
spr.SetColor(cc);
}
//since it's a template, it's really in the h. file. Posted for clarity.
template<class Object>
void Draw(Object &gfx) {
buff.Draw(gfx.spr); // buff is a sf::RenderTexture
buff.Display();
}
void Canvas::Refresh() {
buff.Display();
spr.SetTexture(buff.GetTexture());
}
void Canvas::Clear() {
buff.Clear(CLEAR);
}
Transition structure:
void Fader::Logic() {
if (start.r > target.r) start.r -= speed;
else if (start.r < target.r) start.r += speed;
if (start.g > target.g) start.g -= speed;
else if (start.g < target.g) start.g += speed;
if (start.b > target.b) start.b -= speed;
else if (start.b < target.b) start.b += speed;
if (start.a > target.a) start.a -= speed;
else if (start.a < target.a) start.a += speed;
if (target.r == start.r && target.g == start.g &&
target.b == start.b && target.a == start.a)
GoToNext();
}
void Fader::Input() {
}
void Fader::Drawing() {
Buffer->Clear();
Copy->Blit();
Buffer->Tint(start);
}
void Fader::GoToNext() {
Buffer->Tint();
if (next == END)
Screen->Close();
else if (next == TITLE)
Queue = new Title();
else if (next == ENGINE)
Queue = new Engine();
/* delete this;*/
}
Engine class:
#include "Engine.h"
Engine::Engine() : Started(no) {
you.Make("Ship2.png"); //Calls the constructor of the Graphic structure
you.spr.SetOrigin(14, 14);
}
Engine::~Engine(void) {
}
void Engine::Logic() {
/* Bitmap bmp = Screen->Capture();
bmp.SaveToFile("wtf.png");
*/
}
void Engine::Input() {
static sf::Clock Timer;
if (Pressed.Tap(KEY_ENTER)) {
delete this;
Queue = new Fader(TITLE);
}
if (Pressed.Press(KEY_LEFT))
you.MoveX(-3);
if (Pressed.Hold(KEY_RIGHT))
you.MoveX(+3);
if (Pressed.Hold(KEY_DOWN))
you.MoveY(+3);
if (Pressed.Hold(KEY_UP))
you.Rotate(-5);
Timer.Reset();
}
void Engine::Drawing() {
Buffer->Clear();
Graphic *Sky = new Graphic("Night Sky.png");
Sky->Blit();
you.Blit(); //The error draws this even when commented out! Wut?
delete Sky;
}
Core class:
Irrelevant code...
void Core::Drawing() {
Screen->Clear(); // Screen is a sf::RenderWindow*
Queue->Drawing();
Buffer->Refresh();
Screen->Draw(Buffer->spr);
Screen->Display();
}
Assuming it's not a bug, it has to have something to do with the way I'm clearing and displaying the RenderTexture, likely doing something in the wrong order. Help!