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

Author Topic: Problem when replacing sdl with sfml, kinda :)  (Read 3142 times)

0 Members and 1 Guest are viewing this topic.

Hawy

  • Newbie
  • *
  • Posts: 8
    • View Profile
Problem when replacing sdl with sfml, kinda :)
« on: June 09, 2009, 04:21:00 pm »
Hello


First i create the app window, and then send the window to another class that are supposed to draw something on it.But the window is blank when i run the program.
I can see the image if I make some ugly code and draw an image in the main loop.

Code: [Select]
#include "SDL.h"
#include <string>
#include "Window.h"
#include "Model.h"
#include "SDL_mixer.h"
#include "SDL_ttf.h"
#include <SFML/Graphics.hpp>
 #include <SFML/Audio.hpp>
#include <SFML/System.hpp>



using namespace std;


int main( int argc, char* args[] )
{
        if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }


    //Initialize SDL_ttf
    if( TTF_Init() == -1 ){
         return false;
         }
    Mix_OpenAudio( 44000, MIX_DEFAULT_FORMAT, 2, 4096 );
    sf::RenderWindow *app;
    app = new sf::RenderWindow(sf::VideoMode(800, 600), "SFML window");
     //(sf::VideoMode(800, 600), "SFML window");

    Model *model = new Model();
 //   Window *theWindow = new Window(800,600,model);
    SDL_Event event;
    bool keysHeld[10] = {false};
  //  bool keyPulses[10];
  //  keyPulses[4] = true;
    long start;
 //   SDL_ShowCursor(SDL_DISABLE);


    sf::Image Image;
    Image.LoadFromFile("backgrounds/selectscreen.bmp");
     sf::Sprite Sprite1;

     Sprite1.SetImage(Image);

     // Sprite(Image);




    while( model->quitcheck() == false )
    {

        //If there's an event to handle
        if( SDL_PollEvent( &event ) )
        {
            //If a key was pressed
            if( event.type == SDL_KEYDOWN )
            {
           switch(event.key.keysym.sym) {
            case SDLK_LEFT:
        keysHeld[0] = true;
                 break;
            case SDLK_RIGHT:
        keysHeld[1] = true;
        break;
            case SDLK_UP:
        keysHeld[2] = true;
        break;
            case SDLK_DOWN:
        keysHeld[3] = true;
                 break;
           case SDLK_RETURN:
           //     if (keyPulses[4] == true){
                keysHeld[4] = true;
            //    keyPulses[4] = false;
          //      }

                break;
           case SDLK_ESCAPE:
                keysHeld[5]= true;
                break;
                 }


            }
            if( event.type == SDL_KEYUP )
            {
           switch(event.key.keysym.sym) {
            case SDLK_LEFT:
        keysHeld[0] = false;
                 break;
            case SDLK_RIGHT:
        keysHeld[1] = false;
        break;
            case SDLK_UP:
        keysHeld[2] = false;
        break;
            case SDLK_DOWN:
        keysHeld[3] = false;
                 break;
           case SDLK_RETURN:
                keysHeld[4] = false;
             //   keyPulses[4] = true;
                break;
           case SDLK_ESCAPE:
                keysHeld[5] = false;
                break;
                 }

                 }

        }

       start = SDL_GetTicks();
       model->handleinput(keysHeld);
       model->updateGame();
       model->drawAll(app);
     //  app->Draw(Sprite1);
       app->Display();


    //   theWindow->draw();
       while((start + 16.67) > SDL_GetTicks()){

                   }

        }


    SDL_Quit();

    return 0;
}





Code: [Select]
void Model::drawAll(sf::RenderWindow *screenen){


                 gamestate->drawStagelow(screenen);

}





// Selectscreen is a subclass to gamestate
Code: [Select]
            void SelectScreen::drawStagelow(sf::RenderWindow  *screenen){
                 screenen->Draw(background1);




          //      apply_surface(offsetbackground.x,offsetbackground.y,background,screenen,&test);
          //      apply_surface(offsetstart.x - test.x,offsetstart.y, startbutton,screenen,&test);
           //     apply_surface(offsetquit.x - test.x,offsetquit.y, quitbutton,screenen,&test);
           //     apply_surface(offsetunderline.x - test.x,offsetunderline.y, buttonunderline,screenen,&test);
           //     apply_surface(offsetcontinue.x - test.x,offsetcontinue.y, continuebutton,screenen,&test);
           //     apply_surface(offsetoptions.x - test.x,offsetoptions.y, optionsbutton,screenen,&test);

                       }



SamuraiCrow

  • Newbie
  • *
  • Posts: 40
    • Yahoo Instant Messenger - samuraileumas
    • View Profile
Problem when replacing sdl with sfml, kinda :)
« Reply #1 on: June 09, 2009, 04:57:21 pm »
First, you are checking for SDL events rather than SFML events.  Second, is the window/view in a global variable?  If not, you'll need to pass it as a parameter.  Third, your posted code seems to be awfully confused since it can't decide whether to use SFML or SDL.

Hawy

  • Newbie
  • *
  • Posts: 8
    • View Profile
Problem when replacing sdl with sfml, kinda :)
« Reply #2 on: June 09, 2009, 05:43:18 pm »
The events shouldnt effect the window?
Its not a global variable, im sending it as a parameter

model->drawAll(app);

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Problem when replacing sdl with sfml, kinda :)
« Reply #3 on: June 09, 2009, 07:10:02 pm »
The code is a real mess and full of errors. For example, you never think of freeing allocated objects and put loads of code into a single function.

I suggest you'd consider a good C++ book or tutorial first. There seem to be several basics missing... Next step would be the decision between SDL and SFML.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hawy

  • Newbie
  • *
  • Posts: 8
    • View Profile
Problem when replacing sdl with sfml, kinda :)
« Reply #4 on: June 10, 2009, 12:05:54 pm »
Ye well thats probably true.

Anyway i have narrowed down the problem



Code: [Select]
#include "SDL.h"
#include <string>
#include "Gamestate.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#include <SFML/Graphics.hpp>
 #include <SFML/Audio.hpp>
#include <SFML/System.hpp>
using namespace std;

#ifndef SELECTSCREEN_H
#define SELECTSCREEN_H
//class Model;

class SelectScreen: public Gamestate{
      private:
      sf::Sprite background1;
      sf::Sprite startbutton1;
      sf::Sprite quitbutton1;
      sf::Sprite buttonunderline1;
      sf::Sprite optionsbutton1;
      sf::Sprite continuebutton1;

   





      public:

             SelectScreen(Model *lol);

            ~SelectScreen(){}

           void drawStagelow(sf::RenderWindow  *screenen);
           void handleinput(bool keys[10]);
           void updateStage();
           sf::Sprite getSprite(){return background1;}


      };





#endif
















Code: [Select]
            SelectScreen::SelectScreen(Model *lol) : Gamestate(lol){


                       sf::Image Image;
    Image.LoadFromFile("backgrounds/selectscreen.bmp");
     background1.SetImage(Image);

}










Code: [Select]
            void SelectScreen::drawStagelow(sf::RenderWindow  *screenen){
                 sf::Image Image;
    Image.LoadFromFile("backgrounds/selectscreen.bmp");
     sf::Sprite Sprite1;

     Sprite1.SetImage(Image);


               //dont work   screenen->Draw(background1);
               
               
               //work        screenen->Draw(Sprite1);




     

                       }

I find it weird that only one of them work.

 

anything