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

Author Topic: AniSprite from C++ to C  (Read 9552 times)

0 Members and 1 Guest are viewing this topic.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« on: November 16, 2010, 05:22:55 pm »
Okay, I'm trying to convert that but I can't, first I will show You my translated code. Oryginal code is here:
http://www.sfml-dev.org/wiki/en/sources/anisprite

AniSprite.C:
Code: [Select]

extern "C"
{
#include "AniSprite.h" //PAMIETAC O NAPISNIU sf_Destroyi na koncu!!!
int ZerujDane(AniSprite pedal);
int SetFrameSize(int frameW, int frameH, AniSprite pedal);
int GetFrameCount(AniSprite pedal);
sfIntRect GetFramePosition(int frame, AniSprite pedal);
int SetFrame(int frame, AniSprite pedal);
int SetLoopSpeed(float newfps, AniSprite pedal);
int PlayUstalone(int start, int end, AniSprite pedal);
int PlayZwykle(AniSprite pedal);
int Stop(AniSprite pedal);
int Update(AniSprite pedal);
//int Laduj(AniSprite pedal, int frameW, int frameH);
//int Laduj(AniSprite pedal);
}
int ZerujDane(AniSprite pedal)
{
pedal.duszek = NULL;
pedal.zdjecie = NULL;
pedal.clock = NULL;
pedal.fps = 1;
pedal.currentFrame=0;
pedal.isPlaying=false;
pedal.loopStart=0;
pedal.loopEnd=0;
pedal.frameHeight=0;
pedal.frameWidth=0;
return 1;
}
//int Laduj(AniSprite pedal, int frameW, int frameH)
//{
//pedal.zdjecie = sfImage_CreateFromFile("Character.png"); //SPRAWDZIC JESZCZE POTEm
//pedal.duszek = sfSprite_Create();
//sfSprite_SetImage(pedal.duszek, pedal.zdjecie);
//SetFrameSize(frameW, frameH, pedal);
//return 1;
//}

int SetFrameSize(int frameW, int frameH, AniSprite pedal)
{
pedal.frameWidth = frameW;
pedal.frameHeight = frameH;
sfIntRect pedal2;
pedal2.Left = 0;
pedal2.Top = 0;
pedal2.Right = frameW;
pedal2.Bottom = frameH;
sfSprite_SetSubRect(pedal.duszek,pedal2);
return 1;
}

int GetFrameCount(AniSprite pedal)
{
unsigned int across = (sfImage_GetWidth(pedal.zdjecie) / pedal.frameWidth);
unsigned int down = (sfImage_GetHeight(pedal.zdjecie) / pedal.frameHeight);
return across*down;
}
sfIntRect GetFramePosition(int frame, AniSprite pedal)
{
unsigned int across = (sfImage_GetWidth(pedal.zdjecie) / pedal.frameWidth);
unsigned int down = (sfImage_GetHeight(pedal.zdjecie) / pedal.frameHeight);

int tileY = frame / across;
int tileX = frame % across;
sfIntRect result; // MOZE ZMIENIC NA GLOBALNA ZMIENNA;
result.Left = tileX*pedal.frameWidth;
result.Top = tileY*pedal.frameHeight;
result.Right = tileX*pedal.frameWidth + pedal.frameWidth;
result.Bottom = tileY*pedal.frameHeight + pedal.frameHeight;
return result;
}
int SetFrame(int frame, AniSprite pedal)
{
pedal.currentFrame = frame;
return 1;
}

int SetLoopSpeed(float newfps, AniSprite pedal)
{
pedal.fps = newfps;
return 1;
}
int PlayUstalone(int start, int end, AniSprite pedal)
{
pedal.loopStart = start;
pedal.loopEnd = end;
pedal.currentFrame = start;
pedal.isPlaying = true;
sfClock_Reset(pedal.clock);
return 1;
}
int PlayZwykle(AniSprite pedal)
{
PlayUstalone(0, GetFrameCount(pedal), pedal); //ALBO TRZECI ARGUMENT NULL
return 1;
}
int Stop(AniSprite pedal)
{
pedal.isPlaying = false;
return 1;
}
int Update(AniSprite pedal)
{
if(pedal.isPlaying)
{
int frameCount = pedal.loopEnd - pedal.loopStart;
float timePosition = (sfClock_GetTime(pedal.clock) * pedal.fps);
pedal.currentFrame = pedal.loopStart + (int) timePosition % frameCount;
sfSprite_SetSubRect(pedal.duszek, GetFramePosition(pedal.currentFrame, pedal)); //ALBO TRZECI ARGUMENT NULL
}
return 1;
}


AniSprite.h:
Code: [Select]

#pragma once
extern "C"
{
#include <SFML/Graphics.h>
#include <SFML/Config.h>
#include <SFML/System.h>
typedef struct
{
sfImage* zdjecie;
sfSprite* duszek;
sfClock* clock;
float fps;
bool isPlaying;
int loopStart;
int loopEnd;
int currentFrame;
int frameWidth;
int frameHeight;
} AniSprite;
//int Laduj(AniSprite pedal, int frameW, int frameH);
//int Laduj(AniSprite pedal);
int ZerujDane(AniSprite pedal);
int SetFrameSize(int frameW, int frameH, AniSprite pedal);
int GetFrameCount(AniSprite pedal);
sfIntRect GetFramePosition(int frame, AniSprite pedal);
int SetFrame(int frame, AniSprite pedal);
int SetLoopSpeed(float newfps, AniSprite pedal);
int PlayUstalone(int start, int end, AniSprite pedal);
int PlayZwykle(AniSprite pedal);
int Stop(AniSprite pedal);
int Update(AniSprite pedal);
}


I'm trying to use it like that:
Code: [Select]

extern "C"
{
#include <SFML/System.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include "AniSprite.h"
AniSprite test;
}
#include <iostream>
void Instaluj(sfSprite* duszek, sfImage* zdjecie, char* sciezka);
char* tabela[4] = {"natesty.jpg", "natesty2.jpg", "natesty3.jpg"};
int main()
 {
ZerujDane(test);
test.clock = sfClock_Create();
test.duszek = sfSprite_Create();
test.zdjecie = sfImage_CreateFromFile("character.png");
sfSprite_SetImage(test.duszek, test.zdjecie);
//SetFrameSize(47, 64, test);
int a;
a = SetFrameSize(47, 64, test);
if (a == 1)
{
std::cout << "Nie Spierdala sie" << std::endl;
}
//Laduj(test, 47, 64);
SetLoopSpeed(60, test);
//PlayZwykle(test);
PlayUstalone(4, 7, test);
     sfWindowSettings Settings = {24, 8, 0};
     sfVideoMode Mode = {800, 600, 32};
     sfRenderWindow* App;
     sfImage* Image = NULL;
     sfSprite* Sprite = NULL;
     sfEvent Event;
Sprite = sfSprite_Create();

     /* Create the main window */
     App = sfRenderWindow_Create(Mode, "SFML Testy", sfClose, Settings);
     if (!App)
         return EXIT_FAILURE;
Instaluj(Sprite, Image, tabela[0]);
sfRenderWindow_SetFramerateLimit(App, 60);
     /* Start the game loop */
     while (sfRenderWindow_IsOpened(App))
     {
//std::cout << test.frameHeight << std::endl;
//std::cout << test.frameWidth << std::endl;
         /* Process events */
         while (sfRenderWindow_GetEvent(App, &Event))
         {
             /* Close window : exit */
             if (Event.Type == sfEvtClosed)
                 sfRenderWindow_Close(App);
         }
 
         /* Clear the screen */
         sfRenderWindow_Clear(App, sfBlack);
Update(test);
 
         /* Draw the sprite */
         sfRenderWindow_DrawSprite(App, Sprite);
sfRenderWindow_DrawSprite(App, test.duszek);
 
 
         /* Update the window */
         sfRenderWindow_Display(App);
     }
 
     /* Cleanup resources */
     sfSprite_Destroy(Sprite);
     sfImage_Destroy(Image);
     sfRenderWindow_Destroy(App);
 
     return EXIT_SUCCESS;
 }


Some weird problems:
It shows that
test.frameHeight and test.frameWidth == 0 but why? I called function: SetFrameSize(47, 64, test);
But it shows that SetFrameSize return 1 so it should work...
Anyway when I launch app it runs without crash(woooo before I was crashing like shit) but I can only see:
and it doesn't animate itself.

Other weird shitty problems:
If I use PlayZwykle(test) it crash and shows You devining by 0:
Code: [Select]

int GetFrameCount(AniSprite pedal)
{
unsigned int across = (sfImage_GetWidth(pedal.zdjecie) / pedal.frameWidth); HERE
unsigned int down = (sfImage_GetHeight(pedal.zdjecie) / pedal.frameHeight); HERE
return across*down;
}


I bet it's because test.frameWidth and test.frameHeight are 0 but still can't figure out why....


I know that probably noone of You give shit about C version of SFML but I really do care about it and can't figure it out, if someone could help I would be grateful.

EDIT: THE SAME HAPPENS WITH FPS SETTING NO METTER WHAT I TRY TO SET THROUGHT FUNCTION IT ALWAYS IS 0 AND COMPILER IN COME CASES SAY THAT test structure is not inicialized.


God damn it help people please.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
AniSprite from C++ to C
« Reply #1 on: November 16, 2010, 05:30:56 pm »
Just skimmed trough but saw immediately that you are trying to set the values on the argument but you have to remember, when you exit the function those values are lost. If you want to modify the values on the actual structure that you are passing trough the argument you'll have to do it with a pointer.

Example of what I mean:
Code: [Select]

int ZerujDane(AniSprite pedal)    // <- pedal is not the same as the one you entered as argument
{
   pedal.duszek = NULL;
   pedal.zdjecie = NULL;
   pedal.clock = NULL;
   pedal.fps = 1;
   pedal.currentFrame=0;
   pedal.isPlaying=false;
   pedal.loopStart=0;
   pedal.loopEnd=0;
   pedal.frameHeight=0;
   pedal.frameWidth=0;
return 1;
}  // <- Here the pedal data is lost.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« Reply #2 on: November 16, 2010, 05:42:21 pm »
Yeah, it's probably that.
How change it via pointer I should something like:
AniSprite* wsk = &pedal;
and then:
wsk->fps = new valuable?

And where should I place it?
I should change all function to take pointer as argument instead of whole structure?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
AniSprite from C++ to C
« Reply #3 on: November 16, 2010, 05:45:42 pm »
Quote from: "darekg11"

I should change all function to take pointer as argument instead of whole structure?


That is correct. And then you pass the reference(don't know what it's called in C) of the structure as argument.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« Reply #4 on: November 16, 2010, 05:49:59 pm »
So new version of:
Code: [Select]

int ZerujDane(AniSprite pedal)    
{
   pedal.duszek = NULL;
   pedal.zdjecie = NULL;
   pedal.clock = NULL;
   pedal.fps = 1;
   pedal.currentFrame=0;
   pedal.isPlaying=false;
   pedal.loopStart=0;
   pedal.loopEnd=0;
   pedal.frameHeight=0;
   pedal.frameWidth=0;
return 1;
}  


Should be:
Code: [Select]

AniSprite* wsk=&pedal;
int ZerujDane(AniSprite*pointer)    
{
   wsk->duszek = NULL;
   wsk->zdjecie = NULL;
   wsk->clock = NULL;
   wsk->fps = 1;
   wsk->currentFrame=0;
   wsk->isPlaying=false;
   wsk->loopStart=0;
   wsk->loopEnd=0;
   wsk->frameHeight=0;
   wsk->frameWidth=0;
return 1;
}  

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
AniSprite from C++ to C
« Reply #5 on: November 16, 2010, 06:02:04 pm »
Quote from: "Groogy"
And then you pass the reference(don't know what it's called in C) of the structure as argument.
You mean the structure's address. & is the address-of operator, C has no references.

Quote from: "darekg11"
So new version of [...] Should be [...]
Why the global variable? And why don't you use the parameter pointer? You can just replace the . with -> and change AniSprite to AniSprite*.

Apart from that, returning always the same value is mostly as meaningful as returning none. The return type should rather be void instead of int.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« Reply #6 on: November 16, 2010, 06:09:07 pm »
I will take care of returning right values once this will be fixed, beause still hsa problems.

So it can look like that:
Code: [Select]

int ZerujDane(AniSprite* pointer)
{
pointer->duszek = NULL;
pointer->zdjecie = NULL;
pointer->clock = NULL;
pointer->fps = 1;
pointer->currentFrame=0;
pointer->isPlaying=false;
pointer->loopStart=0;
pointer->loopEnd=0;
pointer->frameHeight=0;
pointer->frameWidth=0;
return 1;
}

In main I use it like:
AniSprite test;
AniSprite* wsk= &test;
 ZerujDane(wsk);

Also one more question, because for exmaple this function:
Code: [Select]

int SetFrameSize(int frameW, int frameH, AniSprite pedal) //FOR NOW ITS NOT FIXED, I KNOW
{
pedal.frameWidth = frameW;
pedal.frameHeight = frameH;
sfIntRect pedal2;
pedal2.Left = 0;
pedal2.Top = 0;
pedal2.Right = frameW;
pedal2.Bottom = frameH;
sfSprite_SetSubRect(pedal.duszek,pedal2);
return 1;
}

Needs sfIntRect structure, I think here it can stay like that, right? Because I don't need to change it value throught function?

And I would liek to really thanks for info and help.

EDIT: One more question:
For example in oryginal code:
Code: [Select]

AniSprite::Play()
{
this->Play(0,GetFrameCount());
}

How Can I do this in C, for now I have:
Code: [Select]

int PlayZwykle(AniSprite pedal)
{
PlayUstalone(0, GetFrameCount(pedal), pedal);
return 1;
} Is it okay?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
AniSprite from C++ to C
« Reply #7 on: November 16, 2010, 06:11:03 pm »
Quote from: "Nexus"
Quote from: "Groogy"
And then you pass the reference(don't know what it's called in C) of the structure as argument.
You mean the structure's address. & is the address-of operator, C has no references.


I didn't mean reference as the C++ construct but as the "foo structure have an reference to bar" which would mean that the foo structure has a pointer to bar. What I didn't know was if C has a special name for it, the address-of operator is not the name of what you actually do.
http://en.wikipedia.org/wiki/Reference_%28computer_science%29
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« Reply #8 on: November 16, 2010, 06:36:33 pm »
Guys, it's working.
BIG THANKS.
I will finish this code up and I will place it here, maybe somebody will need it in the future?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
AniSprite from C++ to C
« Reply #9 on: November 16, 2010, 07:35:45 pm »
Quote from: "darekg11"
Guys, it's working.
BIG THANKS.
I will finish this code up and I will place it here, maybe somebody will need it in the future?


If it's a general way to implement animated sprites then you can probably put it on SFML's Wikipedia.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« Reply #10 on: November 16, 2010, 09:42:24 pm »
Translated code:
AniSprite.h:
Code: [Select]

#pragma once
extern "C"
{
#include <SFML/Graphics.h>
#include <SFML/Config.h>
#include <SFML/System.h>
typedef struct
{
sfImage* zdjecie;
sfSprite* duszek;
sfClock* clock;
float fps;
bool isPlaying;
int loopStart;
int loopEnd;
int currentFrame;
int frameWidth;
int frameHeight;
} AniSprite;
void ZerujDane(AniSprite* pointer);
void SetFrameSize(int frameW, int frameH, AniSprite* pointer);
int GetFrameCount(AniSprite* pointer);
sfIntRect GetFramePosition(int frame, AniSprite* pointer);
void SetFrame(int frame, AniSprite* pointer);
void SetLoopSpeed(float newfps, AniSprite* pointer);
void PlayUstalone(int start, int end, AniSprite* pointer);
void PlayZwykle(AniSprite* pointer);
void Stop(AniSprite* pointer);
void Update(AniSprite* pointer);
}


AniSprite.C
Code: [Select]

extern "C"
{
#include "AniSprite.h"
void ZerujDane(AniSprite* pointer);
void SetFrameSize(int frameW, int frameH, AniSprite* pointer);
int GetFrameCount(AniSprite* pointer);
sfIntRect GetFramePosition(int frame, AniSprite* pointer);
void SetFrame(int frame, AniSprite* pointer);
void SetLoopSpeed(float newfps, AniSprite* pointer);
void PlayUstalone(int start, int end, AniSprite* pointer);
void PlayZwykle(AniSprite* pointer);
void Stop(AniSprite* pointer);
void Update(AniSprite* pointer);
}
void ZerujDane(AniSprite* pointer)
{
pointer->duszek = NULL;
pointer->zdjecie = NULL;
pointer->clock = NULL;
pointer->fps = 1;
pointer->currentFrame=0;
pointer->isPlaying=false;
pointer->loopStart=0;
pointer->loopEnd=0;
pointer->frameHeight=0;
pointer->frameWidth=0;
}

void SetFrameSize(int frameW, int frameH, AniSprite* pointer)
{
pointer->frameWidth = frameW;
pointer->frameHeight = frameH;
sfIntRect pedal2;
pedal2.Left = 0;
pedal2.Top = 0;
pedal2.Right = frameW;
pedal2.Bottom = frameH;
sfSprite_SetSubRect(pointer->duszek,pedal2);
}

int GetFrameCount(AniSprite* pointer)
{
unsigned int across = (sfImage_GetWidth(pointer->zdjecie) / pointer->frameWidth);
unsigned int down = (sfImage_GetHeight(pointer->zdjecie) / pointer->frameHeight);
return across*down;
}
sfIntRect GetFramePosition(int frame, AniSprite* pointer)
{
unsigned int across = (sfImage_GetWidth(pointer->zdjecie) / pointer->frameWidth);
unsigned int down = (sfImage_GetHeight(pointer->zdjecie) / pointer->frameHeight);

int tileY = frame / across;
int tileX = frame % across;
sfIntRect result;
result.Left = tileX*pointer->frameWidth;
result.Top = tileY*pointer->frameHeight;
result.Right = tileX*pointer->frameWidth + pointer->frameWidth;
result.Bottom = tileY*pointer->frameHeight + pointer->frameHeight;
return result;
}
void SetFrame(int frame, AniSprite* pointer)
{
pointer->currentFrame = frame;
}

void SetLoopSpeed(float newfps, AniSprite* pointer)
{
pointer->fps = newfps;
}
void PlayUstalone(int start, int end, AniSprite* pointer)
{
pointer->loopStart = start;
pointer->loopEnd = end;
pointer->currentFrame = start;
pointer->isPlaying = true;
sfClock_Reset(pointer->clock);
}
void PlayZwykle(AniSprite* pointer)
{
PlayUstalone(0, GetFrameCount(pointer), pointer);
}
void Stop(AniSprite* pointer)
{
pointer->isPlaying = false;
}
void Update(AniSprite* pointer)
{
if(pointer->isPlaying)
{
int frameCount = pointer->loopEnd - pointer->loopStart;
float timePosition = (sfClock_GetTime(pointer->clock) * pointer->fps);
pointer->currentFrame = pointer->loopStart + (int) timePosition % frameCount;
sfSprite_SetSubRect(pointer->duszek, GetFramePosition(pointer->currentFrame, pointer));
}
}

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
AniSprite from C++ to C
« Reply #11 on: November 16, 2010, 09:48:15 pm »
I recommend that you create a page here with the code: http://www.sfml-dev.org/wiki/en/sources?DokuWiki=e9912b226137a37e23ca7510f0bd2125
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
AniSprite from C++ to C
« Reply #12 on: November 16, 2010, 09:51:14 pm »
Yeah I will do it later, thanks.