#include <SDL.h>
#include <EGL/egl.h>
#include<vector>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <sstream>
#include <GLES/gl.h>
#include<string>
#include <SDL2/SDL.h>
struct kulka { int a; int b; } ball={0, 0} , wall= {1 , 1} , speed= {3, 4}, quad;
SDL_Rect enemy;
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *bitmapTex = NULL;
SDL_Surface *surface; SDL_Surface * kursor = NULL; SDL_Surface * przycisk = NULL; /*area exit*/SDL_Rect przyciskDane; SDL_Rect przyciskCel; /*arena start*/SDL_Rect startButonW; SDL_Rect startButonH; /*rect of player and ball*/
SDL_Rect rect, darea; SDL_Rect hit; SDL_Rect shot; /*mouse postion*/SDL_Rect kursorDane; /*suirface*/SDL_Surface *bitmapSurface = NULL; SDL_Event event;
/* -----------| | */
void triangle (SDL_Renderer *renderer) { int a;
przyciskDane.w = 150; przyciskDane.h = 50; przyciskCel.x = 0; przyciskCel.y = 0;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); /* E */SDL_RenderDrawLine(renderer, 0, 0, 50, 0); SDL_RenderDrawLine(renderer, 0, 25, 25, 25);
SDL_RenderDrawLine(renderer, 0, 50, 50, 50);
SDL_RenderDrawLine(renderer, 0, 0, 0, 50); /* X */
SDL_RenderDrawLine(renderer, 50, 0, 100, 50);
SDL_RenderDrawLine(renderer, 100, 0, 50, 50); /* I */SDL_RenderDrawLine(renderer, 100, 0, 100, 50); /* T */SDL_RenderDrawLine(renderer, 100, 0, 150, 0);
SDL_RenderDrawLine(renderer, 125, 0, 125, 50);
}
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 speed.b=-3;
}
}
void start (SDL_Renderer *renderer) { int a; startButonW.w = 225; startButonW.h = 150; startButonH.x = 0; startButonH.y = 100;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); /* S */SDL_RenderDrawLine(renderer, 0, 100, 50, 100);
SDL_RenderDrawLine(renderer, 0, 100, 0, 125); SDL_RenderDrawLine(renderer, 0, 125, 50, 125);
SDL_RenderDrawLine(renderer, 0, 150, 50, 150);
SDL_RenderDrawLine(renderer, 50, 125, 50, 150); /* T */SDL_RenderDrawLine(renderer, 50, 100, 100, 100);
SDL_RenderDrawLine(renderer, 75, 100, 75, 150); /* A */
SDL_RenderDrawLine(renderer, 100, 150, 125, 100);
SDL_RenderDrawLine(renderer, 125, 100, 150, 150); /* R */SDL_RenderDrawLine(renderer, 150, 150, 150, 100);
SDL_RenderDrawLine(renderer, 150, 100, 175, 100); SDL_RenderDrawLine(renderer, 175, 100, 175, 125);
SDL_RenderDrawLine(renderer, 175, 125, 150, 125);
SDL_RenderDrawLine(renderer, 150, 125, 175, 150); /* T */SDL_RenderDrawLine(renderer, 175, 100, 225, 100);
SDL_RenderDrawLine(renderer, 200, 100, 200, 150);
} 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 point (SDL_Renderer *renderer) {
// kursorDane.y ;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); SDL_RenderDrawLine(renderer, ball.a, ball.b, ball.a+50, ball.b+50);
SDL_RenderDrawLine(renderer, ball.a+50, ball.b, ball.a+50, ball.b+50); //SDL_RenderDrawLine(renderer, a, b, a+50, b+50); SDL_RenderDrawLine(renderer, ball.a+50, ball.b , ball.a, ball.b); ball.a+=speed.a;
ball.b+=speed.b; if (ball.a>600) {--speed.a;} if (ball.b>600) {--speed.b;} if(ball.b<0) {++speed.b;} if(ball.a<0) {++speed.a;}
} void
DrawChessBoard(SDL_Renderer * renderer)
{
/* Get the Size of drawing surface */
SDL_RenderGetViewport(renderer, &darea);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
shot.w =10 ;
shot.h =10 ;
shot.x = ball.a ;
shot.y=ball.b ;
ball.b+=speed.b; ball.a+=speed.a;
if (ball.a>200) {--speed.a;} if (ball.b>400) {--speed.b;} if(ball.b<0) {speed.b++;} if(ball.a<0) {speed.a++;}
SDL_RenderFillRect(renderer, &shot);
}
void
player(SDL_Renderer * renderer)
{
/* Get the Size of drawing surface */SDL_RenderGetViewport(renderer, &darea); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
hit.w =50 ;
hit.h =10 ;
hit.x = kursorDane.x ;
hit.y=300 ;
SDL_RenderFillRect(renderer, &hit);
CheckCollision(shot, hit);
}
void drawEnemy(SDL_Renderer *render) {
SDL_RenderGetViewport(renderer, &darea);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
enemy.w=25;
enemy.h=25;
enemy.x=130;
enemy.y=130;
SDL_RenderFillRect(renderer, &enemy);
CheckCollision(shot, enemy);
}
int main(int argc, char *argv[]) {
SDL_Init( SDL_INIT_EVERYTHING ); int posX = 100, posY = 100, width = 320, height = 240;
SDL_RenderFillRect(renderer, &rect);
// SDL_Init(SDL_INIT_VIDEO);
win = SDL_CreateWindow("Hello World", 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); // SDL_WM_SetCaption( "Zdarzenia", NULL ); // SDL_Flip( ekran );
while (1) { SDL_ShowCursor( SDL_DISABLE ); SDL_Event e; if( e.type == SDL_KEYDOWN ) { switch( e.key.keysym.sym ) { case SDLK_RIGHT: ball.a+=1; break; case SDLK_LEFT: ball.a-=1; break; } }
if( e.type == SDL_MOUSEMOTION ) { kursorDane.x = e.motion.x; kursorDane.y = e.motion.y;
if(kursorDane.x>ball.a&&kursorDane.y>ball.b&&kursorDane.x<ball.a+50&&kursorDane.y<ball.b+50) { --speed.a;
ball.a=kursorDane.x;
ball.b=kursorDane.y;
}
} //If a key was released
else if( e.type == SDL_KEYUP ) { switch( e.key.keysym.sym ) {
case SDLK_RIGHT: ball.a+=10; break; case SDLK_LEFT: ball.a-=10; break; } }
if( e.type == SDL_MOUSEBUTTONDOWN ) {
if( e.button.button == SDL_BUTTON_LEFT && ( kursorDane.x >= przyciskCel.x && kursorDane.x <= przyciskCel.x + przyciskDane.w ) &&( kursorDane.y >= przyciskCel.y && kursorDane.y <= przyciskCel.y + przyciskDane.h ) ) { e.type==SDL_QUIT; break; } if( e.button.button == SDL_BUTTON_LEFT && ( kursorDane.x >= startButonH.x && kursorDane.x <= startButonH.x + startButonW.w ) &&( kursorDane.y >= startButonH.y && kursorDane.y <= startButonH.y + startButonW.h ) ) {
//glClear(GL_COLOR_BUFFER_BIT);
//triangle (renderer) ;
line(renderer); point(renderer);
glFlush(); SDL_GL_SwapWindow(win);
} } if (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
break;
}
}
DrawChessBoard(renderer);
start (renderer);
triangle (renderer) ;
line(renderer);
player(renderer);
drawEnemy(renderer);
// point(renderer);
/* Got everything on rendering surface,
now Update the drawing image on window screen */
SDL_UpdateWindowSurface(win);
SDL_RenderFillRect(renderer, &rect);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
// SDL_RenderPresent(renderer);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // turn on animate
// glClear(GL_COLOR_BUFFER_BIT);
//SDL_GL_SwapWindow(win);
}
SDL_DestroyTexture(bitmapTex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
No comments:
Post a Comment