VPN termux about China.net

Rpg game sdl try

 #include <GLES/gl.h>

#include <stdio.h>

#include <stdlib.h>

#include <SDL2/SDL.h>

#include <SDL_image.h>


const int SIZE = 11;

const int SCREEN_WIDTH = SIZE * 60;

const int SCREEN_HEIGHT = SIZE * 30;

const int AREA = SCREEN_WIDTH * SCREEN_HEIGHT;

SDL_Window *window = NULL;

SDL_Renderer *renderer = NULL;

SDL_Surface *backgroundImage = NULL;

SDL_Event event;

SDL_Texture *background;



/*for player*/

struct Player {

    int x=300, y=600;

    int size = 20;

}neo,neo_speed;


void draw_neo(){

        SDL_Rect playerRect = { neo.x,neo.y, neo.size, neo.size };

        SDL_SetRenderDrawColor(renderer, 0, 0, 00, 5);

        SDL_RenderFillRect(renderer, &playerRect);

    }






/*for pad*/

SDL_Rect mouse;

SDL_Rect upButton;

    SDL_Rect downButton;

    SDL_Rect leftButton;

    SDL_Rect rightButton;

void pad_initialize() {

    // Ustaw przykładowe położenie i rozmiary przycisków

    upButton.x = 300;

    upButton.y = 750;

    upButton.w = 150;

    upButton.h = 150;


    downButton.x = 300;

    downButton.y = 1000;

    downButton.w = 150;

    downButton.h = 150;


    leftButton.x = 100;

    leftButton.y = 900;

    leftButton.w = 150;

    leftButton.h = 150;


    rightButton.x = 500;

    rightButton.y = 900;

    rightButton.w = 150;

    rightButton.h = 150;

}

bool CheckCollision(SDL_Rect r1, SDL_Rect r2)

{

// Simplified collision check using rectangle coordinates

return (r1.x < r2.x + r2.w && r1.x + r1.w > r2.x && r1.y < r2.y + r2.h && r1.y + r1.h > r2.y);

}


void Pad_render() {

pad_initialize();

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); // czarny kolor


    SDL_RenderFillRect(renderer, &upButton);

    SDL_RenderFillRect(renderer, &downButton);

    SDL_RenderFillRect(renderer, &leftButton);

    SDL_RenderFillRect(renderer, &rightButton);

}

bool GetKeys()

{

    while (SDL_PollEvent(&event) != 0)

    {

        if (event.type == SDL_QUIT)

        {

            return 1;

        }

        if (event.type == SDL_KEYDOWN)

        {

       

        }

        if (event.type == SDL_MOUSEMOTION)

        {

            mouse.x = event.motion.x;

            mouse.y = event.motion.y;

        }                        // If a key was released

        if (event.type == SDL_MOUSEBUTTONDOWN)

        {

            if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(leftButton, mouse))

            {                //SDL_SetWindowPosition(window, +150, 100);

          neo.x-=10;

            //neo.y-=10;

            break;

            }

            if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(rightButton, mouse))

            {

neo.x+=10;

            }

            if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(upButton, mouse))

            {

      neo.y-=10;

            }

            if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(downButton, mouse))

            {

neo.y+=10;

            }

        }

    }

    return 0;

}


/*for map*/

SDL_Rect squares[99];

const int SQUARE_SIZE = 180;

int map[9 * 9] = {

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    1, 1, 1, 1, 1, 1, 1, 1, 1,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

    0, 0, 0, 1, 0, 0, 0, 0, 0,

};

unsigned direcao = 0;

void maps(){

for (int i = 0; i < 99; ++i)

{

squares[i].x = (i % 9) * SQUARE_SIZE;

squares[i].y = (i / 9) * SQUARE_SIZE;

squares[i].w = SQUARE_SIZE;

squares[i].h = SQUARE_SIZE;

}

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0); // Red background

SDL_RenderClear(renderer);


for (int i = 0; i < 81; ++i)

{

if (map[i] == 1)

{

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);

SDL_RenderFillRect(renderer, &squares[i]);

}

if (map[i] == 0)

{

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

SDL_RenderFillRect(renderer, &squares[i]);

}

if (map[i] == 2)

{

SDL_SetRenderDrawColor(renderer, 255, 5, 5, 20);

SDL_RenderFillRect(renderer, &squares[i]);


}

}

}

void go()

{


}


void player(SDL_Renderer * renderer)

{                        

}

void color()

{

           SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);

}

bool InitWindow()

{

    int imgFlag = IMG_INIT_PNG;

    IMG_Init(imgFlag);

    if (SDL_Init(SDL_INIT_VIDEO) < 0)

    {

        printf("ERRO\n");

        return false;

    }

    else

    {        window =

            SDL_CreateWindow("rpg", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,

                             SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);

        if (window == NULL)

        {

            printf("ERRO\n");

            return false;

        }

        else

        {

            renderer =

                SDL_CreateRenderer(window, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

        }

    }


    if ((backgroundImage = SDL_LoadBMP("/sdcard/pobrane.png")) == NULL)

        printf("ERRO %s", SDL_GetError());

    background = SDL_CreateTextureFromSurface(renderer, backgroundImage);

    return true;

}

void GameLoop(bool & quit)

{

    quit = GetKeys();

    SDL_RenderClear(renderer);

    go();     

    SDL_RenderCopy(renderer, background, NULL, NULL);

    maps();

    color();

    draw_neo();

    Pad_render();

    SDL_RenderPresent(renderer);

}


void Close()

{

    SDL_DestroyWindow(window);

    window = NULL;

    SDL_DestroyTexture(background);

    SDL_FreeSurface(backgroundImage);

    SDL_DestroyRenderer(renderer);

    IMG_Quit();

    SDL_Quit();

}


int main(int argc, char *argv[])

{

    bool quit = false;

     InitWindow();

    while (!quit)

    {

        GameLoop(quit);

    }

    Close();

    return 0;

}

No comments:

Post a Comment