Seite 1 von 1

animation left to right problem

Verfasst: Mi Mai 29, 2013 11:38 am
von hardcoding
Hallo,

Ja ich bin es wieder mit einer neuen SDL technischen Frage. :)
Ich habe das Problem,dass er ein Rechteck von Rechts nach links blittet. Ich will genau das Umgekehrte.(Von Links nach Rechts)

Hier ist der Code:

Code: Alles auswählen

Animation.h

Code: Alles auswählen

#ifndef _ANIMATION_
#define _ANIMATION_

class animation{

private:
    int vetikal,geschwindigkeit,frame,status;
    int startmember,pausemember,pausmember,starmember;

public:

    animation();
    void start();
    void loadimages();
    void update();
    void autobewegung();
    void Move();
    void applysurface(int x, int y, SDL_Surface *quelle, SDL_Surface *ziel, SDL_Rect *clip);
    void show();
    void clips();
    int getticks();


};

#endif

Code: Alles auswählen

Animation.cpp

Code: Alles auswählen

include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "animation.h"

const int width          = 640;
const int height         = 480;
const int framepersecond = 10;
const int foowidth       = 64;
const int fooheight      = 205;
const int fooright       = 0;
const int fooleft        = 1;


SDL_Rect clipsRight[ 4 ];

SDL_Surface *bild = NULL;
SDL_Surface *hintergrund = NULL;
SDL_Surface *reihee = NULL;

animation::animation()
{
    startmember = 0,pausemember = 0;
    pausmember = false,starmember = false;
    vetikal = 0;
    geschwindigkeit = 0;
    frame = 0;
    status = fooright;
}

void animation::start()
{
    starmember=true;
    pausmember=false;
    startmember=SDL_GetTicks();
}

int animation::getticks()
{
    if( starmember == true )
    {
        return SDL_GetTicks() - startmember;
    }
}

void animation::applysurface(int x, int y, SDL_Surface *quelle, SDL_Surface *ziel, SDL_Rect *clip = NULL)
{
    SDL_Rect offset;
    offset.x = 900;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(quelle,clip,ziel,&offset);
}

void animation::clips()
{
     //Clip the sprites
    clipsRight[ 0 ].x = 0;
    clipsRight[ 0 ].y = 0;
    clipsRight[ 0 ].w = foowidth;
    clipsRight[ 0 ].h = fooheight;

    clipsRight[ 1 ].x = foowidth;
    clipsRight[ 1 ].y = 0;
    clipsRight[ 1 ].w = foowidth;
    clipsRight[ 1 ].h = fooheight;

    clipsRight[ 2 ].x = foowidth * 2;
    clipsRight[ 2 ].y = 0;
    clipsRight[ 2 ].w = foowidth;
    clipsRight[ 2 ].h = fooheight;

    clipsRight[ 3 ].x = foowidth * 3;
    clipsRight[ 3 ].y = 0;
    clipsRight[ 3 ].w = foowidth;
    clipsRight[ 3 ].h = fooheight;


}

void animation::Move()
{
    vetikal+=geschwindigkeit;

}


void animation::show()
{
     if( geschwindigkeit < 0 )
    {
        status = fooleft;

        frame++;
    }
    else
    {
        frame = 0;
    }

    if( frame >= 4 )
    {
        frame = 0;
    }

    if( status == fooright )
    {
        applysurface( vetikal,height - fooheight, reihee, hintergrund, &clipsRight[ frame] );
    }
}

void animation::loadimages()
{
    hintergrund = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);
    bild = IMG_Load("d5.jpg");
    reihee = IMG_Load("reihe.jpg");
    SDL_BlitSurface(bild,0,hintergrund,0);

}

void animation::update()
{
     SDL_Flip(hintergrund);
}


void animation::autobewegung()
{
    int z = 16;
    geschwindigkeit+=z;

}

Code: Alles auswählen

main.cpp

Code: Alles auswählen

#include <iostream>
#include <SDL/SDL.h>
#include "SDL/SDL_image.h"
#include "animation.h"

const int framepersecond = 10;


SDL_Event event;

int main( int argc, char* args[])
{
    bool quit = false;
    SDL_Init(SDL_INIT_EVERYTHING);
    animation ani;
    ani.loadimages();
    ani.clips();


    while( quit == false )
    {
        ani.start();

        while( SDL_PollEvent( &event ) )
        {
            //Handle events for the stick figure
            ani.autobewegung();

            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

        ani.Move();
        ani.show();
        ani.update();

        if( ani.getticks() < 1000 / framepersecond )
        {
            SDL_Delay( ( 6000 / framepersecond ) - ani.getticks() );
        }

    }
    return 0;
}
Ich vermute dass, das Problem hier liegt:

Code: Alles auswählen

void animation::Move()
{
    vetikal+=geschwindigkeit;

}
oder hier:

Code: Alles auswählen

void animation::applysurface(int x, int y, SDL_Surface *quelle, SDL_Surface *ziel, SDL_Rect *clip = NULL)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(quelle,clip,ziel,&offset);
}
Ich habe das besen rumgepfuscht. Hat aber alles irgendwie nichts gebracht.
Habe das Code noch hochgeladen zum ausprobieren.

Re: animation left to right problem

Verfasst: Mi Mai 29, 2013 1:17 pm
von Xin
Nur kurz überflogen:

In Autobewegung ist z positiv und wird immer auf Geschwindigkeit addiert.
Geschwindigkeit wird an der Stelle also immer irgendwann positiv.

In show() stellst Du fest, dass status == fooleft wird, wenn geschwindigkeit < 0.
ApplySurface rufst Du nur auf, wenn status == fooright. Wenn Du also eine negative Geschwindigkeit hast, passiert auf dem Bildschirm nix mehr?

Re: animation left to right problem

Verfasst: Mi Mai 29, 2013 1:31 pm
von hardcoding
Xin hat geschrieben: In Autobewegung ist z positiv und wird immer auf Geschwindigkeit addiert.
Geschwindigkeit wird an der Stelle also immer irgendwann positiv.

In show() stellst Du fest, dass status == fooleft wird, wenn geschwindigkeit < 0.
ApplySurface rufst Du nur auf, wenn status == fooright. Wenn Du also eine negative Geschwindigkeit hast, passiert auf dem Bildschirm nix mehr?
Mit fooright und fooleft war überflüssig.
Mittlerweile habe ich das überarbeitet.

Code: Alles auswählen

void animation::autobewegung()
{
    int z = -16;
    geschwindigkeit-= z;
}

Code: Alles auswählen

void animation::Move()
{
    vetikal+=geschwindigkeit;
}

Code: Alles auswählen

void animation::show()
{

    frame++;
    if( frame >= 4 )
    {
        frame = 0;
    }
    applysurface( vetikal,height - fooheight, reihee, hintergrund, &clipsRight[ frame] );

}
trotzdessen tut sich nichts. Ich habe das Gefühl das er SDL_Blitsurface die x auf x=0 setzt.

Re: animation left to right problem

Verfasst: Mi Mai 29, 2013 2:07 pm
von Xin
hardcoding hat geschrieben:

Code: Alles auswählen

void animation::autobewegung()
{
    int z = 16;
    geschwindigkeit+=z;
}
Mittlerweile habe ich das überarbeitet.

Code: Alles auswählen

void animation::autobewegung()
{
    int z = -16;
    geschwindigkeit-= z;
}
trotzdessen tut sich nichts. Ich habe das Gefühl das er SDL_Blitsurface die x auf x=0 setzt.
Wo ist der Unterschied zwischen den beiden Varianten von autobewegung()?

Re: animation left to right problem

Verfasst: Mi Mai 29, 2013 3:33 pm
von hardcoding
Erste Variante :

Code: Alles auswählen

int z = 16; geschwindigkeit+=z; vetikal+=geschwindigkeit; = 16
Sry zweite Variante ist so gemeint:

Code: Alles auswählen

int z = 16; geschwindigkeit-= z; vetikal+=geschwindigkeit; = -16
Trotzdem tut sich nichts.

Re: animation left to right problem

Verfasst: Do Mai 30, 2013 4:13 pm
von hardcoding
Jemand noch eine Idee ? :)

Re: animation left to right problem

Verfasst: Fr Mai 31, 2013 8:16 pm
von Xin
hardcoding hat geschrieben:Jemand noch eine Idee ? :)
Ich bin momentan verhindert, wenn Du bis Dienstag nicht weiterkommst und noch Hilfe wünschst, stell mir bitte ein ZIP Archiv mit Makefile oder einer halbwegs bekannten IDE zusammen, wenn das geht. Dazu ein Readme, welche Libs benötigt werden, bzw. im Makefile ein Ziel, dass mit apt-get die benötigten Sachen installiert, sofern nicht vorhanden.

Vorher komme ich leider zu nichts Produktivem.

Re: animation left to right problem

Verfasst: Mo Jun 03, 2013 9:43 pm
von hardcoding
Xin hat geschrieben:
hardcoding hat geschrieben:Jemand noch eine Idee ? :)
Ich bin momentan verhindert, wenn Du bis Dienstag nicht weiterkommst und noch Hilfe wünschst, stell mir bitte ein ZIP Archiv mit Makefile oder einer halbwegs bekannten IDE zusammen, wenn das geht. Dazu ein Readme, welche Libs benötigt werden, bzw. im Makefile ein Ziel, dass mit apt-get die benötigten Sachen installiert, sofern nicht vorhanden.

Vorher komme ich leider zu nichts Produktivem.
Hachh wo bleibt Kerli. :D
Ehrlich gesagt habe ich den code seit drei Tagen nicht bearbeitet. Finde arduinos spannender. :lol:

Re: animation left to right problem

Verfasst: Mo Jun 03, 2013 11:06 pm
von Kerli
Ganz verstehe ich noch nicht was du erreichen möchtest. Du solltest aber vermutlich bei jedem update bevor du das Rechteck zeichnest den Hintergrund neu blitten, da sonst ja der Bereich den das Rechteck vorher verdeckt hat noch wieder korrekt den Hintergrund anzeigen wird.

Re: animation left to right problem

Verfasst: Mo Jun 03, 2013 11:08 pm
von hardcoding
Gelöst. :idea: Aber trotzdem danke für deine Hilfe Kerli.