Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved]Why does this not work (Sprite SetImage problem)  (Read 3040 times)

0 Members and 1 Guest are viewing this topic.

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
[Solved]Why does this not work (Sprite SetImage problem)
« on: April 11, 2010, 03:59:03 pm »
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:
Code: [Select]

    Player p(SHIP);
// OR
    p.assign(SHIP);
doesn't work.

It displays a white box where the ship would be.


main.cpp
Code: [Select]

#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
Code: [Select]
#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
Code: [Select]
#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.

JollyRoger

  • Newbie
  • *
  • Posts: 17
    • View Profile
[Solved]Why does this not work (Sprite SetImage problem)
« Reply #1 on: April 11, 2010, 05:59:01 pm »
You'll either want to pass the image to the player by reference, or with a pointer.

Try changing your player code to:
Code: [Select]

Player(Image& i);

void assign(Image& i);

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
[Solved]Why does this not work (Sprite SetImage problem)
« Reply #2 on: April 11, 2010, 11:28:55 pm »
Thank you very much.

Unrelated question but here it goes. I want to do a simple animation, will it be efficient to store an array of Sprites in every instance of the object? Each Sprite being clipped an IntRect( I guess ) from a specified Image.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
[Solved]Why does this not work (Sprite SetImage problem)
« Reply #3 on: April 13, 2010, 02:16:52 am »
Quote from: "hagel"
Thank you very much.

Unrelated question but here it goes. I want to do a simple animation, will it be efficient to store an array of Sprites in every instance of the object? Each Sprite being clipped an IntRect( I guess ) from a specified Image.
It will run faster than just creating them dynamically and it shouldn't use too much memory, so yes. Although it might save a little memory to create a global set of sprites for the animation, it probably wouldn't help too much.
I use the latest build of SFML2