Adding left, right, up, and down arrow keys
This commit is contained in:
parent
7447bb52b2
commit
979dff9c6c
136
main.cpp
136
main.cpp
@ -1,43 +1,71 @@
|
|||||||
|
#define SDL_MAIN_HANDLED
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
#include <SDL_image.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
void draw_line(SDL_Point source, SDL_Point destination, SDL_Renderer * rend)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_maze(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int main( int argc, const char* argv[] )
|
int main( int argc, const char* argv[] )
|
||||||
{
|
{
|
||||||
|
SDL_Renderer *renderer = NULL;
|
||||||
|
SDL_Texture *texture = NULL;
|
||||||
|
SDL_Surface *bitmapSurface = NULL;
|
||||||
SDL_Window *win;
|
SDL_Window *win;
|
||||||
SDL_Surface *image;
|
SDL_Surface *image;
|
||||||
|
SDL_Rect srcR, destR;
|
||||||
Uint32 rmask, gmask, bmask, amask;
|
Uint32 rmask, gmask, bmask, amask;
|
||||||
const char name[] = "BLAH";
|
const char name[] = "Game";
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||||
{
|
{
|
||||||
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
|
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
win = SDL_CreateWindow(name, 0, 0, 500, 300, SDL_WINDOW_RESIZABLE);
|
win = SDL_CreateWindow(name, 0, 0, 800, 800, SDL_WINDOW_RESIZABLE);
|
||||||
if (win == NULL) {
|
if (win == NULL)
|
||||||
|
{
|
||||||
// In the case that the window could not be made...
|
// In the case that the window could not be made...
|
||||||
printf("Could not create window: %s\n", SDL_GetError());
|
printf("Could not create window: %s\n", SDL_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
||||||
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||||
|
SDL_Rect rectangle;
|
||||||
|
|
||||||
image = SDL_GetWindowSurface(win);
|
rectangle.x = 0;
|
||||||
printf("Pixel Width: %d, Pixel Heigth: %d\n", image->w, image->h);
|
rectangle.y = 0;
|
||||||
// image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGB444, 0);
|
rectangle.w = 50;
|
||||||
//Update the surface
|
rectangle.h = 50;
|
||||||
Uint32 *pixels = (Uint32 *)image->pixels;
|
SDL_RenderFillRect(renderer, &rectangle);
|
||||||
SDL_UpdateWindowSurface( win );
|
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 800);
|
||||||
|
srcR.h = 800;
|
||||||
|
srcR.w = 800;
|
||||||
|
destR.h = 800;
|
||||||
|
destR.w = 800;
|
||||||
|
srcR.x = 0;
|
||||||
|
srcR.y = 0;
|
||||||
|
destR.x = 0;
|
||||||
|
destR.y = 0;
|
||||||
bool run = true;
|
bool run = true;
|
||||||
while (run) {
|
while (run)
|
||||||
|
{
|
||||||
clock_t start_t, end_t;
|
clock_t start_t, end_t;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
bool redraw = false;
|
||||||
|
while (SDL_PollEvent(&event))
|
||||||
|
{
|
||||||
/* handle your event here */
|
/* handle your event here */
|
||||||
if (event.type == SDL_MOUSEBUTTONDOWN)
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
||||||
{
|
{
|
||||||
@ -47,28 +75,90 @@ int main( int argc, const char* argv[] )
|
|||||||
}
|
}
|
||||||
else if (event.button.button == SDL_BUTTON_LEFT) // scroll up
|
else if (event.button.button == SDL_BUTTON_LEFT) // scroll up
|
||||||
{
|
{
|
||||||
//Update the surface
|
rectangle.x += 50;
|
||||||
SDL_LockSurface(image);
|
rectangle.y += 0;
|
||||||
for (size_t i = 0; i < (500*300); i++) {
|
redraw = true;
|
||||||
pixels[i] += 0xFF;
|
|
||||||
}
|
}
|
||||||
SDL_UnlockSurface(image);
|
|
||||||
start_t = clock();
|
|
||||||
SDL_UpdateWindowSurface(win);
|
|
||||||
end_t = clock();
|
|
||||||
printf("Time to draw a frame %f \n", ((double)(end_t - start_t)/(double)(CLOCKS_PER_SEC)));
|
|
||||||
}
|
}
|
||||||
|
else if (event.type == SDL_KEYDOWN)
|
||||||
|
{
|
||||||
|
if (event.key.keysym.sym == SDLK_UP)
|
||||||
|
{
|
||||||
|
rectangle.y -= 50;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_DOWN)
|
||||||
|
{
|
||||||
|
rectangle.y += 50;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_RIGHT)
|
||||||
|
{
|
||||||
|
rectangle.x += 50;
|
||||||
|
}
|
||||||
|
else if (event.key.keysym.sym == SDLK_LEFT)
|
||||||
|
{
|
||||||
|
rectangle.x -= 50;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Do nothing. */
|
||||||
|
}
|
||||||
|
redraw = true;
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_QUIT)
|
else if (event.type == SDL_QUIT)
|
||||||
{
|
{
|
||||||
run = false;
|
run = false;
|
||||||
}
|
}
|
||||||
|
else if (event.type == SDL_WINDOWEVENT)
|
||||||
|
{
|
||||||
|
switch (event.window.event)
|
||||||
|
{
|
||||||
|
case SDL_WINDOWEVENT_ENTER:
|
||||||
|
case SDL_WINDOWEVENT_SHOWN:
|
||||||
|
case SDL_WINDOWEVENT_MOVED:
|
||||||
|
case SDL_WINDOWEVENT_EXPOSED:
|
||||||
|
case SDL_WINDOWEVENT_RESTORED:
|
||||||
|
redraw = true;
|
||||||
|
break;
|
||||||
|
case SDL_WINDOWEVENT_RESIZED:
|
||||||
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||||
|
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||||
|
redraw = true;
|
||||||
|
SDL_GetWindowSize(win, &destR.w, &destR.h);
|
||||||
|
SDL_Texture *texture_temp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, destR.w, destR.h);
|
||||||
|
srcR.h = destR.h;
|
||||||
|
srcR.w = destR.w;
|
||||||
|
SDL_RenderCopy(renderer, texture_temp, &srcR, &destR);
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
texture = texture_temp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Do nothing. */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (redraw)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
SDL_SetRenderTarget(renderer, texture);
|
||||||
|
SDL_RenderDrawRect(renderer, &rectangle);
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0x00);
|
||||||
|
SDL_RenderFillRect(renderer, &rectangle);
|
||||||
|
SDL_SetRenderTarget(renderer, NULL);
|
||||||
|
SDL_RenderCopy(renderer, texture, &srcR, &destR);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
/* do some other stuff here -- draw your app, etc. */
|
/* do some other stuff here -- draw your app, etc. */
|
||||||
}
|
}
|
||||||
SDL_FreeSurface(image);
|
SDL_FreeSurface(image);
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(win);
|
SDL_DestroyWindow(win);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
//g++ main.cpp -o blah `sdl2-config --cflags --libs`
|
//g++ main.cpp -o blah `sdl2-config --cflags --libs`
|
||||||
|
/*
|
||||||
|
g++ main.cpp -IC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\include\SDL2 -LC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o SDLMain
|
||||||
|
*/
|
Loading…
Reference in New Issue
Block a user