Hey people
I "just" started using SFML and I am working on a SHMUP
I started by creating some nice functions to load images. But this:
Player p(SHIP);
// OR
p.assign(SHIP);
doesn't work.
It displays a white box where the ship would be.
main.cpp
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "player.h"
#include <iostream>
#include <string.h>
using namespace sf;
using namespace std;
/***
*
* int bulletLevel=0;
* game.shootBullet( *OWNER* player, *TYPE* player.bulletLevel);
* * Game spawnar då de nögvändiga bulletsen och skjuter dom åt OWNERn *
*
*
***/
Image loadSprite(string s);
int main(){
RenderWindow app(VideoMode(160, 240, 32), "test::sfml");
app.SetFramerateLimit(60);
const Input& Input = app.GetInput();
Clock clock;
float rate;
Event event;
Image PlayerShip = loadSprite("gfx/ship.png");
Image SHIP;
SHIP.LoadFromFile("gfx/ship.png");
SHIP.CreateMaskFromColor(Color(255,0,255));
SHIP.SetSmooth(false);
Sprite s(SHIP);
Sprite f(PlayerShip);
Player p;
p.assign(SHIP);
while (app.IsOpened()){
rate = app.GetFrameTime();
while (app.GetEvent(event)){
if (event.Type == Event::Closed)
app.Close();
if ((event.Type == Event::KeyPressed) && (event.Key.Code == Key::Escape))
app.Close();
}
if (app.GetInput().IsKeyDown(Key::Left)) p.move(4,1);
if (app.GetInput().IsKeyDown(Key::Right)) p.move(6,1);
if (app.GetInput().IsKeyDown(Key::Up)) p.move(8,1);
if (app.GetInput().IsKeyDown(Key::Down)) p.move(2,1);
p.update();
app.Clear(Color(35, 35, 46));
p.draw(app);
//app.Draw(s);
app.Display();
}
return EXIT_SUCCESS;
}
Image loadSprite(string s){
Image i;
if (!i.LoadFromFile(s)){ printf("WHATTA"); }
i.CreateMaskFromColor(Color(255,0,255));
i.SetSmooth(false);
return i;
};
player.h
#ifndef PLAYER_H_
#define PLAYER_H_
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <string>
using namespace sf;
class Player{
public:
int x,y;
int velx,vely;
int speed;
int w,h;
int cx,cy;
int shoot_speed;
int dir;
Sprite frame;
Player();
Player(Image i);
void update();
void move(int direction, int type);
void assign(Image i);
void nextFrame();
bool off_screen();
void draw(RenderWindow &app);
};
#endif
player.cpp
#include "player.h"
#include <string>
#include <iostream>
#define LEFT 4
#define RIGHT 6
#define DOWN 2
#define UP 8
#define TILE_SIZE 16
Player::Player(){
x=32;
y=128;
velx=0;
vely=0;
};
Player::Player(Image i){
x=32;
y=128;
velx=0;
vely=0;
frame.SetImage(i);
frame.SetPosition(x,y);
};
void Player::assign(Image i){
frame.SetImage(i);
frame.SetPosition(x,y);
}
void Player::move(int direction, int type){
dir = direction;
if(direction == LEFT){
velx=-1;
} else if(direction == RIGHT) {
velx=1;
} else if(direction == DOWN) {
vely=1;
} else if(direction == UP) {
vely=-1;
}
};
void Player::update(){
x+=velx;
y+=vely;
velx=0;
vely=0;
printf("%i %i \n",x,y);
};
void Player::draw(RenderWindow &app){
frame.SetX(x);
frame.SetY(y);
app.Draw(frame);
};
Thanks for your time.
PS The images seem to load properly but does not get assigned to the Sprite.