Не могу сделать плавное движение квадрата, с использованием SDL 1.0

Пишу простую игру, с использованием SDL 1.0. Хочу, чтобы квадрат двигался в определенную сторону, при зажатии клавиши, например w. Подскажите, как это реализовать, с учетом того, что движение должно быть привязано к таймеру. Ниже мой код.

#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include <SDL/SDL_draw.h>
#include <SDL/SDL_ttf.h>
SDL_Surface* screen;



Uint8 *keystate = SDL_GetKeyState(NULL);
int c = 0;
int a, b;
int X = 600;
int Y = 300;
int afg;

struct BUTTON
{
    int w = 200;
    int h = 100;
};

struct SCREEN
{
    //Размер окна
    int w = 1200;
    int h = 600;

    //Границы
    int x1 = 1;
    int x2 = 1199;

    int y1 = 1;
    int y2 = 599;

    //Границы нижней панели
    int border = 450;
};

class Player
{
public:
    int ammunition = 180;
    int xp = 0;
    int hp = 100;
    int x = 50;
    int y = 50;

    //размеры главного квадрата
    int w = 25;
    int h = 25;
    //размеры оружия
    int w1 = 10;
    int h1 = 5;

    void Move()//пока не робит
    {
        //Uint8 *keystate = SDL_GetKeyState(NULL);

        //while(SDL_PollEvent())
        //Draw_Rect(screen, X, Y, w, h, SDL_MapRGB(screen->format, 255, 255, 255));
        if(keystate[SDLK_w] == 1) Y -= 10;


    }

};

int main_menu(void);
int new_game(void);
int face_1(int time_m, int time, int ammunition, int xp, int hp);

int main ( int argc, char** argv )
{

    if(SDL_Init(SDL_INIT_EVERYTHING) != 0) return 1234;
    while(c != 404)
    {
        switch(c)
        {
        case 0:
            main_menu();
            break;
        case 1:
            new_game();

            break;
        case 100:
            printf("Error 100\n");
            break;
        case 101:
            printf("Error 101\n");
            break;
        case 102:
            printf("Error 102\n");
            break;
        }
    }
    return 0;
}

int face_1(int time_m, int time, int ammunition, int xp, int hp)
{
    SCREEN razmer;
    Player igrok;


    SDL_Event event;
    SDL_Rect dst;

    SDL_Surface* text_surface_1 = NULL;
    SDL_Color text_color_1 = {153, 51, 0};

    TTF_Font* fnt = NULL;

    char text[255];

    fnt = TTF_OpenFont("CharisSILR.ttf", 29);
    if(!fnt)
    {
        TTF_Quit();
        SDL_Quit();
        return 301;
    }

    SDL_putenv("SDL_VIDEO_CENTERED = true");
    screen = SDL_SetVideoMode(1200, 600, 32, SDL_SWSURFACE);
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 133, 133, 133));

    //Нижняя панель
    dst.x = 0;
    dst.y = razmer.border;
    dst.w = 1200;
    dst.h = 200;
    SDL_FillRect(screen, &dst, SDL_MapRGB(screen->format, 60,60,60));

    //Кнопка выхода в меню. Пока не работает
    dst.x = 300;
    dst.y = razmer.border;
    dst.w = 600;
    dst.h = 200;
    SDL_FillRect(screen, &dst, SDL_MapRGB(screen->format, 153,51,0));


    //TIME
    Draw_HLine(screen, 10, 500, 200, SDL_MapRGB(screen->format, 153, 51, 0));
    sprintf(text, "Time: %02d:%02d", time_m,time);
    if(text_surface_1 = TTF_RenderUTF8_Solid(fnt, text, text_color_1))
    {
        dst.x = 10;
        dst.y = 450;
        SDL_BlitSurface(text_surface_1, NULL, screen, &dst);
        SDL_FreeSurface(text_surface_1);
        text_surface_1 = NULL;
    }

    //AMMUNITION
    Draw_HLine(screen, 10, 550, 200, SDL_MapRGB(screen->format, 153, 51, 0));
    sprintf(text, "Ammunition: %d", ammunition);
    if(text_surface_1 = TTF_RenderUTF8_Solid(fnt, text, text_color_1))
    {
        dst.x = 10;
        dst.y = 500;
        SDL_BlitSurface(text_surface_1, NULL, screen, &dst);
        SDL_FreeSurface(text_surface_1);
        text_surface_1 = NULL;
    }

    //XP
    Draw_HLine(screen, 1000, 550, 1190, SDL_MapRGB(screen->format, 153, 51, 0));
    sprintf(text, "XP: %d", xp);
    if(text_surface_1 = TTF_RenderUTF8_Solid(fnt, text, text_color_1))
    {
        dst.x = 1000;
        dst.y = 500;
        SDL_BlitSurface(text_surface_1, NULL, screen, &dst);
        SDL_FreeSurface(text_surface_1);
        text_surface_1 = NULL;
    }

    //HP
    Draw_HLine(screen, 1000, 500, 1190, SDL_MapRGB(screen->format, 153, 51, 0));
    sprintf(text, "HP: %d", hp);
    if(text_surface_1 = TTF_RenderUTF8_Solid(fnt, text, text_color_1))
    {
        dst.x = 1000;
        dst.y = 450;
        SDL_BlitSurface(text_surface_1, NULL, screen, &dst);
        SDL_FreeSurface(text_surface_1);
        text_surface_1 = NULL;
    }

    //Отрисовка квадрата
    Draw_Rect(screen, X, Y, 50, 50, SDL_MapRGB(screen->format, 255,255,255));

    SDL_Flip(screen);

}

int new_game(void)
{

    SCREEN Screen;
    Player igrok;

    SDL_Event event;
    SDL_Event event_w;
    SDL_Event event_a;
    SDL_Event event_s;
    SDL_Event event_d;
    SDL_Event event_nother;

    int ms = 0;
    int interval;
    int s = 0;
    int m = 0;

    int trigger;
    //Uint8 *keystate = SDL_GetKeyState(NULL);

    int b = SDL_GetTicks();
    interval = b - ms;

    int x = Screen.w/2;
    int y = Screen.h/2;
    while(m!=70)
    {

        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) return c = 0;
            if(keystate[SDLK_w] == 1) trigger = 1;
        }

        ms = SDL_GetTicks() - interval;
        s = ms / 1000;

        if(s % 60 == 0 && s != 0)//если уже 60 секунд, то ...
        {
            m += 1;// ... к минуте прибавляется 1
            s = 0;// ... секунды обнуляются
            interval += 60000;/* ... к интервалу прибавляется 1 минута, чтобы секунды корректно отображались*/
        }

        if(ms % 50 == 0)//если прошло 50 миллисекунд, то ...
        {
            if(trigger == 1) Y -= 1;//... если trigger=1, тогда у Y отнимается 1
            face_1(m,s,igrok.ammunition,igrok.xp,igrok.hp);//... рисуем поверхность
        }

    }
    return c = 404;
}

int main_menu(void)
{
    SCREEN razmer;
    BUTTON knopka;

    SDL_Surface* Main_Menu = NULL;
    SDL_Event event;
    SDL_Rect dst;

    SDL_Surface* text_surface = NULL;
    SDL_Color text_color = {0,0,0};
    TTF_Font* fnt = NULL;

    char New_Game[] = "New game";
    char Exit[] = "Exit";

    if(SDL_Init(SDL_INIT_EVERYTHING))
    {
        SDL_Quit();
        TTF_Quit();
        return c = 100;
    }

    if(TTF_Init())
    {
        SDL_Quit();
        TTF_Quit();
        return c = 101;
    }

    fnt = TTF_OpenFont("CharisSILR.ttf", 29);
    if(!fnt)
    {
        SDL_Quit();
        TTF_Quit();
        return c = 102;
    }

    SDL_putenv("SDL_VIDEO_CENTERED=true");
    screen = SDL_SetVideoMode(razmer.w, razmer.h, 32, SDL_SWSURFACE);


    Main_Menu = SDL_LoadBMP("Main_Menu.bmp");

    SDL_BlitSurface(Main_Menu, NULL, screen, NULL);

    a = razmer.w/2 - knopka.w/2;
    b = razmer.h/2 - knopka.h;

    Draw_FillRect(screen, a, b - 100, knopka.w, knopka.h, SDL_MapRGB(screen -> format, 133, 133, 133));
    Draw_FillRect(screen, a, b + 150, knopka.w, knopka.h, SDL_MapRGB(screen -> format, 133, 133, 133));

    if(text_surface = TTF_RenderUTF8_Solid(fnt, New_Game, text_color))
    {
        dst.x = a + 30;
        dst.y = b - 80;
        SDL_BlitSurface(text_surface, NULL, screen, &dst);
        SDL_FreeSurface(text_surface);
        text_surface = NULL;
    }

    if(text_surface = TTF_RenderUTF8_Solid(fnt, Exit, text_color))
    {
        dst.x = a + 75;
        dst.y = b + 170;
        SDL_BlitSurface(text_surface, NULL, screen, &dst);
        SDL_FreeSurface(text_surface);
        text_surface = NULL;
    }

    SDL_Flip(screen);

    while(SDL_WaitEvent(&event))
    {
        if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT)
        {
            int x = event.button.x, y = event.button.y;
            if(x > a && x < (a + knopka.w) && y > (b + 150) && y < (b + knopka.h + 150))//выход
            {
                c = 404;
                SDL_FreeSurface(screen);
                return c;
            }
            if(x > a && x < (a + knopka.w) && y > (b - 100) && y < (b + knopka.h - 100))
            {
                c = 1;
                SDL_FreeSurface(screen);
                return c;
            }

        }
    }
    return c = 404;
}

UPD 1. Уточню. Мне надо, чтобы при удержании клавиши, координаты рисования квадрата менялись. Держу клавишу w - квадрат перерисовывается вверх. Отжал клавишу w - квадрат стоит на месте.


Ответы (0 шт):