/*colision line and triangle and game*/
#include "SDL.h"
#include <SDL.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <SDL2/SDL.h>
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *bitmapTex = NULL;
SDL_Surface *surface;
SDL_Surface * kursor = NULL;
SDL_Surface * przycisk = NULL;
SDL_Rect przyciskDane;
SDL_Rect przyciskCel;
SDL_Rect rect, darea;
SDL_Rect kursorDane;
SDL_Surface *bitmapSurface = NULL;
SDL_Event event;
/* -----------
|
|
*/
void drawtriangle (SDL_Renderer *renderer)
{
int a;
przyciskDane.w = 200;
przyciskDane.h = 50;
przyciskCel.x = 300;
przyciskCel.y = 240;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, 600, 200, 300, 240);
SDL_RenderDrawLine(renderer, 300, 240, 340, 240);
SDL_RenderDrawLine(renderer, 340, 240, 320, 200);
}
int a=0,b=0,c=320,d=240;
int s=0;
void line (SDL_Renderer *renderer)
{
// kursorDane.y ;
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, kursorDane.x, kursorDane.y , kursorDane.x +100, kursorDane.y +100);
}
void
DrawChessBoard(SDL_Renderer * renderer)
{
drawtriangle (renderer);
int row = 0,column = 0,x = 0;
SDL_Rect rect, darea;
/* Get the Size of drawing surface */
SDL_RenderGetViewport(renderer, &darea);
for ( ; row < 8; row++) {
column = row%2;
x = column;
for ( ; column < 4+(row%2); column++) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
rect.w = darea.w/8;
rect.h = darea.h/8;
rect.x = x * rect.w;
rect.y = row * rect.h;
x = x + 2;
SDL_RenderFillRect(renderer, &rect);
}
}
}
void handle_events()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: a+=10; break;
case SDLK_LEFT: a-=10; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: a+=10; break;
case SDLK_LEFT: a-=10; break;
}
}
}
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, 1, 0, 0, 0xFF);
bitmapSurface = SDL_LoadBMP("hello.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 )
{
//Set the velocity
switch( e.key.keysym.sym )
{
case SDLK_RIGHT: a+=10; break;
case SDLK_LEFT: a-=10; break;
}
}
if( e.type == SDL_MOUSEMOTION )
{
kursorDane.x = e.motion.x;
kursorDane.y = e.motion.y;
}
//If a key was released
else if( e.type == SDL_KEYUP )
{
//Set the velocity
switch( e.key.keysym.sym )
{
case SDLK_RIGHT: a+=10; break;
case SDLK_LEFT: 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 (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
break;
}
}
// DrawChessBoard(renderer);
drawtriangle (renderer);
line(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, 1.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