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

Author Topic: applying sprite to sfRenderWindow using a reference in c++  (Read 6778 times)

0 Members and 1 Guest are viewing this topic.

red436

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
applying sprite to sfRenderWindow using a reference in c++
« on: January 09, 2013, 01:19:59 am »
I have a class that uses a reference to sfRenderWindow to render a sprite from a map container. The code compiles and runs fine but won't show the sprite. My code is as follows.

class imageHandler : public Unpacker
{
 
    map<string, sfImage*> imageList;
    map<string, sfSprite*> spriteList;
    public:
    sfRenderWindow* screen;
    imageHandler();
    ~imageHandler();
    void setScreen(sfRenderWindow*& screens);
    void addImage( string imageName );
    void setAlpha( string name, int alpha );
    void clearImages();
    void surfApp( int x, int y, string imageName );
    void surfApp( int X, int Y, string Name,  sfIntRect aniRect);
    int imageW( string pFilename );  
    int imageH( string pFilename );
};
 

#include "ImageHandling.h"

imageHandler::imageHandler(){
      int count = 1;
      int i;

      sfImage** imageList;
      sfSprite** spriteList;
}
imageHandler::~imageHandler(){
}
void imageHandler::setScreen(sfRenderWindow*& screens) {
      screen = screens;
}
void imageHandler::addImage( string imageName)
{
   
      sfImage* image;
      map<string, sfImage*>::iterator i = imageList.find( imageName );
      map<string, sfSprite*>::iterator p = spriteList.find( imageName );
      if( i == imageList.end() )
      {

         image = loadImageFromRes("data.pck", imageName.c_str());
         i = imageList.insert( i, make_pair( imageName, image ) );

      }
      if( p == spriteList.end() )
      {
         sfSprite* sprite = sfSprite_Create();
         sfSprite_SetImage( sprite, image );
         p = spriteList.insert(p, make_pair( imageName, sprite ) );
      }

}
void imageHandler::setAlpha( string name, int alpha )
{
    sfColor alphaAdjust;
    alphaAdjust.r = 255;
    alphaAdjust.g = 255;
    alphaAdjust.b = 255;
    alphaAdjust.a = alpha;
    map<string, sfSprite*>::iterator a = spriteList.find( name );
    if( a != spriteList.end() )
    {
        sfSprite_SetColor( a -> second, alphaAdjust );
    }
}
void imageHandler::surfApp( int x, int y, string imageName )
{
   map<string, sfSprite*>::iterator p = spriteList.find( imageName );
   if( p != spriteList.end() )
   {
       sfSprite_Move( p -> second, x, y );
       sfRenderWindow_DrawSprite( screen, p -> second );
   }
}
void imageHandler::surfApp( int X, int Y, string Name,  sfIntRect aniRect)
{
   map<string, sfSprite*>::iterator p = spriteList.find( Name );
   if( p != spriteList.end() )
   {
      sfSprite_Move(p -> second, X, Y );
      sfSprite_SetSubRect( p -> second, aniRect );
      sfRenderWindow_DrawSprite( screen, p -> second );
   }
};
int imageHandler::imageW(string pFilename)
{
      int result;
      map<string, sfImage*>::iterator p = imageList.find( pFilename );
      if( p != imageList.end() )
      {
         result = sfImage_GetWidth( p -> second );
      }
      return result;
}
int imageHandler::imageH(string pFilename)
{
    int result;
    map<string, sfImage*>::iterator p = imageList.find( pFilename );
    if( p != imageList.end() )
    {
       result = sfImage_GetHeight( p -> second );
    }
    return result;
}
 
main.cpp
#include "Engine.h"
 
int addButtonX = 40;
int addButtonY = 40;
 
sfEvent eventHolder;
 
int addAniX, addAniY = 25;
 
int dirX=1;
int dirY=4;
 
bool quit = false;
bool downupDown = true;
bool downupLeft = true;
bool downupRight = true;
 
int main(int argc, char* args[])
{
   
    imageHandler images;
    Events myEvents;
    Timer fps;
     
    images.addImage("logo.png");
     
    sfVideoMode videoParams;
    videoParams.Width = 640;
    videoParams.Height = 480;
    sfWindowSettings sfSettings;
    sfSettings.AntialiasingLevel = 0;
    sfSettings.DepthBits = 32;
    sfSettings.StencilBits = 0;
    videoParams.BitsPerPixel = 32;
    sfColor colorBlue;
    colorBlue.r = 0;
    colorBlue.g = 0;
    colorBlue.b = 255;
    sfRenderWindow* App = sfRenderWindow_Create( videoParams, "Window Test", sfTitlebar | sfClose, sfSettings  );
 
    while( sfRenderWindow_IsOpened( App ) )
    {
           fps.startTimer();
           images.setScreen(App);
           myEvents.eventStruct( eventHolder );
           while(sfRenderWindow_GetEvent( App, &eventHolder ))
           {
               if( eventHolder.Type == sfEvtClosed )
               {
                   sfRenderWindow_Close( App );
               }
               if( eventHolder.Key.Type == sfEvtKeyPressed )
               {
                   if( eventHolder.Key.Code == sfKeyNum0 )
                   {
                       cout << "Zero pressed" << endl;
                   }
                   if( eventHolder.Key.Code == sfKeyA )
                   {
                       cout << "A key pressed" << endl;
                   }
                   if( eventHolder.Key.Code == sfKeyEscape )
                   {
                      cout << "Esc key pressed" << endl;
                   }
               }
               if( myEvents.keyUp("j") == true )
               {
                   cout << "J key pressed" << endl;
               }
           }
           if( downupDown == false )
           {
              addButtonY += 10;
           }
           images.surfApp( addButtonX, addButtonY, "logo.png" );
           sfRenderWindow_Clear( App, colorBlue );
       
           sfRenderWindow_Display( App );
    }
     
    return 0;
}
« Last Edit: January 09, 2013, 07:59:07 am by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10506
    • View Profile
    • development blog
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #1 on: January 09, 2013, 01:41:37 am »
Why would use SFML's C binding and try to program in C++? ??? :o ??? :o :-\ ???
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

red436

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #2 on: January 09, 2013, 02:14:14 am »
I find the C binding less confusing to use for one. Two this is a port of an engine I made for SDL and didn't want
amjor rewrite. Constructive feedback instead of knocking my binding choice would be nice.

red436

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #3 on: January 09, 2013, 02:40:03 am »
Nevermind I figured it out I was clearing the screen before applying the sprite and using sfSprite_Move() instead of sfSprite_SetPosition() thanks anyway.  :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #4 on: January 09, 2013, 07:58:10 am »
This advice was relevant. You're really making your life a lot more difficult. CSFML has the exact same API as SFML, except that its written the C way. So it forces you to manage memory manually, work with pointers, use ugly structures and functions because of the lack of polymorphism and overload, etc.

It wouldn't be a major rewrite to port it to SFML, you would just have to rename things and remove unnecessary code.

But anyway, I'm glad you solved your problem :)

Next time please use the code=cpp tag to format your code blocks on the forum.
Laurent Gomila - SFML developer

red436

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #5 on: January 09, 2013, 02:19:40 pm »
Laurent, thank you for pointing out the code=cpp tag. I will use it it from now on. Yes, CSFML and SFML appear to be the same but you as as an SFML dev putting it so eloquently "It forces you to manage memory manually, work with pointers, use ugly structures and functions because of the lack of polymorphism and overload, etc."
. The same things more or less were said about switching and combining ASM to and from C. I am looking for
control as well as a learning experience in my ventures. As far as pointers, functions, memory, and structures
go we as software engineers use these every day and need to learn to use them properly. Or we run the the risk
of creating vulnerabilities and kinks in general. Major rewrite? Typing out long scope names of classes as opposed to just typing functions suits me just fine. As to what eXpl0it3r said "Why would use SFML's C binding and try to program in C++?" this is not advice but rather a sarcastic question. Enough of my rant, back to my programming.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #6 on: January 09, 2013, 02:29:03 pm »
Following your point of view, your whole code would be written in C. It's really weird to decide to use a C-style library in a C++ application -- and that's what eXpl0it3r said, I'm sure he didn't mean to be sarcastic ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6283
  • Thor Developer
    • View Profile
    • Bromeon
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #7 on: January 09, 2013, 02:51:31 pm »
I am looking for control as well as a learning experience in my ventures. As far as pointers, functions, memory, and structures go we as software engineers use these every day and need to learn to use them properly. Or we run the the risk of creating vulnerabilities and kinks in general.
Of course we all have to learn them, also in C++. The problem is, even if you are an expert, low-level mechanisms are still more error-prone than abstractions. Here, C++ does a very good job -- you can internally hack the hell out of your code, but wrap everything into a beautiful interface which is safe and easy to use.

A simple example is RAII (possible with constructors and destructors): Here, I have explained in-depth why alternatives will always be more error-prone, less readable, and not even faster. In C, you typically need more code to achieve the same functionality as in C++, and big software projects are more difficult to maintain because of the lack of abstraction features. In the end, you need more development and debugging time.

C has its strengths in hardware-near programming, but not in game development, which amounts to a big part to software design :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

red436

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #8 on: January 10, 2013, 06:40:21 am »
Laurent and Nexus you both make great points. My intention was not to create a flame war here, just to point out the justifications of my personal programming decisions. As you can see from my code I am wrapping the
C style code into easy to use C++ classes. I may consider using RAII in the classes that will be deriving from
these ones. You are right my C code will be more error prone, slower, and probably be less readable. The reason for that is that i'm not an expert and don't claim to be only that i'm learning through this method. C++ as am sure you are aware is a superset of C and the great features we enjoy in C++ have to be implemented in lower levels. C++ code is also translated into ASM which is then compiled into hex instructions. Therefore C++ is
just an abstraction of tried and true opcode coding algorithms brought to us by those before us. I'm sorry if I offended any of you and look forward to working and disscussing further issues with all of you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10506
    • View Profile
    • development blog
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #9 on: January 10, 2013, 07:04:33 am »
I guess we all know how the abstraction levels work and that C++ gets also compiled to ASM, but that doesn't make it more logical to me, to use C++ as primary language, then get the C binding of a C++ library and write C++ wrapper for it.  ???

SFML is a C++ library and the C binding merely exists to provide a interface to easier port/bind SFML to other languages, but writing a binding/wrapper for C++ onto of the C binding of the original C++ library is just, ehrm wrong? ;D

Don't get me wrong, you can do whatever you want and whatever feels better to you, but I probably will never understand it. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #10 on: January 10, 2013, 07:52:08 am »
Quote
C++ as am sure you are aware is a superset of C
Strictly speeking, it's not. Although several new features are taken from one and integrated into the other, now they follow their own separate path.

Quote
C++ code is also translated into ASM which is then compiled into hex instructions. Therefore C++ is
just an abstraction
... which makes code easier to write and less error prone than with direct machine or C instructions ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6283
  • Thor Developer
    • View Profile
    • Bromeon
Re: applying sprite to sfRenderWindow using a reference in c++
« Reply #11 on: January 10, 2013, 10:12:36 am »
We don't feel offended, don't worry :)
And I don't think it's a flame war, as all people seem to discuss nicely ;)

If you want to learn C++, I wouldn't choose C. A lot of concepts of C have been replaced in C++, and you risk to learn old techniques.

But C is also interesting. To learn C, you would learn it more intensely, if you used C for your whole program, not only for SFML. But you have to be careful that you don't mix both languages.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything