#include <GLES/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <vector>
/*turial vector 1
what is vector ? vector is c++ std lib*/
using namespace std;
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 *win = NULL;
SDL_Texture *bitmapTex = NULL;
SDL_Rect rect, darea;
SDL_Surface *surface;
SDL_Window *window = NULL;
SDL_Surface *bitmapSurface;
SDL_Renderer *renderer = NULL;
SDL_Surface *backgroundImage = NULL;
SDL_Event event;
SDL_Texture *background = NULL;
/* kursor */
SDL_Surface *kursor = NULL;
/* draw kursor rect */
SDL_Rect kursorDane;
/* needs 4 rect to use direct event mouse */
//SDL_Rect butonL;
//SDL_Rect butonP;
//SDL_Rect butonU;
//SDL_Rect butonD;
const int CIMA = 1;
const int BAIXO = 2;
const int ESQUERDA = 3;
const int DIREITA = 4;
unsigned direcao = 0;
/*define simple vector of <T>
where T is class or struct*/
vector <SDL_Rect> boxL(1);
SDL_Rect * butonL = boxL.data();
vector <SDL_Rect> boxP(1);
SDL_Rect * butonP = boxP.data();
vector <SDL_Rect> boxU(1);
SDL_Rect * butonU = boxU.data();
vector <SDL_Rect> boxD(1);
SDL_Rect * butonD = boxD.data();
/*part.1 use DATA*/
struct food
{
SDL_Rect posLivre;
int x;
int y;
} food;
struct snake
{
/* head postion */
int x;
int y;
/* try this */
snake *boody;
/* catch this */
SDL_Rect segmento[AREA];
/* how long is snace */
int totalSegmento = 2;
/* ??? */
int dimensao;
} snake, head =
{
20, 2}, speed =
{
2, 2};
bool BodyCollision(int x, int y)
{
for (int i = 2; i < snake.totalSegmento; i++)
if ((snake.segmento[i].x == x) && (snake.segmento[i].y == y))
return true;
return false;
}
void line(SDL_Renderer * renderer)
{ /* draw kursor mouse */
SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, kursorDane.x, kursorDane.y, kursorDane.x + 100,
kursorDane.y + 100);
}
void go()
{
switch (direcao)
{
case DIREITA:
head.y += speed.y;
break;
case ESQUERDA:
head.y -= speed.y;
break;
case CIMA:
head.x += speed.x;
break;
case BAIXO:
head.x -= speed.x;
break;
}
}
/* generator radnnom postion food */
int RandomNumber(int high)
{
return (rand() % (high));
}
/* initiation postion food */
void foodint()
{
food.x = RandomNumber(600);
food.y = RandomNumber(300);
}
/* draw food */
SDL_Rect InitFood(SDL_Renderer * renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
food.posLivre.x = food.x;
food.posLivre.y = food.y;
food.posLivre.w = SIZE;
food.posLivre.h = SIZE;
SDL_RenderFillRect(renderer, &food.posLivre);
return food.posLivre;
}
bool CheckCollision(SDL_Rect r1, SDL_Rect r2)
{
if (r1.x > r2.x + r2.w)
{
return false;
}
else if (r1.y > r2.y + r2.h)
{
return false;
}
else if (r1.x + r1.w < r2.x)
{
return false;
}
else if (r1.y + r1.h < r2.y)
{
return false;
}
else
{
return true;
}
}
int pad(SDL_Renderer * render)
{
butonP->x = 200;
butonP->y = 400;
butonP->w = 66;
butonP->h = 66;
butonL->x = 000;
butonL->y = 400;
butonL->w = 66;
butonL->h = 66;
butonU->y = 300;
butonU->x = 100;
butonU->w = 66;
butonU->h = 66;
butonD->x = 100;
butonD->y = 400;
butonD->w = 66;
butonD->h = 66;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
SDL_RenderFillRect(renderer, &butonD[0]);
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
SDL_RenderFillRect(renderer, &butonU[0]);
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
SDL_RenderFillRect(renderer, &butonL[0]);
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
SDL_RenderFillRect(renderer, &butonP[0]);
}
void player(SDL_Renderer * renderer)
{ /* this func draw rectangle of PLAYER in post
hit.x hit.y and width 50 height 10 */
SDL_Rect hit;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
hit.w = 10;
hit.h = 10;
snake.segmento[0].x = head.x;
snake.segmento[0].y = head.y;
snake.segmento[0].w = SIZE;
snake.segmento[0].h = SIZE;
for (int i = snake.totalSegmento; i > 0; i--)
{
snake.segmento[i] = snake.segmento[i - 1];
}
if (CheckCollision(snake.segmento[0], food.posLivre))
{
snake.totalSegmento += 11;
foodint();
}
if (BodyCollision(head.x, head.y))
{
SDL_Quit();
}
for (int i = snake.totalSegmento; i > 0; i--)
{
/* SDL_RenderCopy(renderer, snake.segmentTexture, &snake.sprite[0],
&snake.segmento[i]); */
SDL_RenderFillRect(renderer, &snake.segmento[i]);
} /* check collision with ball and player */
if (head.x > SCREEN_WIDTH)
{
head.x = 0;
}
if (head.y > SCREEN_HEIGHT)
{
head.y = 0;
}
if (head.x < 0)
{
head.x = SCREEN_WIDTH;
}
if (head.y < 0)
{
head.y = SCREEN_HEIGHT;
}
}
int InitWindow()
{
try
{
SDL_Init(SDL_INIT_EVERYTHING);
int posX = 100, posY = 100, width = 320, height = 240;
SDL_RenderFillRect(renderer, &rect);
win = SDL_CreateWindow("Snake", posX, posY, width, height, SDL_WINDOW_OPENGL);
surface = SDL_GetWindowSurface(win);
renderer = SDL_CreateSoftwareRenderer(surface);
SDL_RenderGetViewport(renderer, &darea);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
bitmapSurface = SDL_LoadBMP("background.bmp");
bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
SDL_FreeSurface(bitmapSurface);
}
catch(int c)
{
printf("ERRO\n");
printf("ERRO %s", SDL_GetError());
}
}
bool GetKeys()
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
return 1;
}
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
if (direcao != DIREITA)
direcao = ESQUERDA;
break;
case SDLK_DOWN:
if (direcao != ESQUERDA)
direcao = DIREITA;
break;
case SDLK_RIGHT:
if (direcao != BAIXO)
direcao = CIMA;
break;
case SDLK_LEFT:
if (direcao != CIMA)
direcao = BAIXO;
break;
}
}
if (event.type == SDL_MOUSEMOTION)
{
kursorDane.x = event.motion.x;
kursorDane.y = event.motion.y;
} // If a key was released
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(butonL[0], kursorDane))
{
if (direcao != CIMA)
direcao = BAIXO;
break;
}
if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(butonP[0], kursorDane))
{
/* right */
if (direcao != BAIXO)
direcao = CIMA;
break;
}
if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(butonD[0], kursorDane))
{
if (direcao != ESQUERDA)
direcao = DIREITA;
break;
}
if (event.button.button == SDL_BUTTON_LEFT && CheckCollision(butonU[0], kursorDane))
{
if (direcao != DIREITA)
direcao = ESQUERDA;
break;
}
}
}
return 0;
}
void GameLoop(bool & quit)
{
SDL_UpdateWindowSurface(win);
quit = GetKeys();
go();
SDL_RenderFillRect(renderer, &rect);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
line(renderer);
pad(renderer);
InitFood(renderer);
player(renderer);
/* background color if tex ==null */
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderPresent(renderer);
}
void Close()
{
SDL_DestroyWindow(win);
win = NULL;
SDL_DestroyTexture(bitmapTex);
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