Added basic panning.

This commit is contained in:
2022-10-29 00:43:17 -05:00
parent 1cb3ba87f8
commit 4018c8bad2
6 changed files with 129 additions and 1 deletions
+168
View File
@@ -0,0 +1,168 @@
#include <SDL2/SDL_surface.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_hints.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_thread.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
int MouseDown = 0;
typedef struct {
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font* font;
} App;
App app;
void HandleInput(void);
int Exit = 0;
int XRel, YRel;
int XOrg, YOrg;
void Cleanup(void) {
SDL_DestroyWindow(app.window);
SDL_DestroyRenderer(app.renderer);
TTF_CloseFont(app.font);
TTF_Quit();
SDL_Quit();
}
int main(int argc, char** argv){
memset(&app, 0, sizeof(app));
XRel = 0;
YRel = 0;
XOrg = 0;
YOrg = 0;
atexit(Cleanup);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL failed: %s\n", SDL_GetError());
return -1;
}
if (TTF_Init() < 0) {
printf("Failed to init TTF: %s\n", TTF_GetError());
return -1;
}
app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14);
if (app.font == NULL) {
printf("Failed to load font\n");
return -1;
}
app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (app.window == NULL) {
printf("Window creation failed: %s\n", SDL_GetError());
return -1;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_ACCELERATED);
if (app.renderer == NULL) {
printf("Renderer failed: %s\n", SDL_GetError());
return -1;
}
//SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_Rect rect;
rect.x = SCREEN_WIDTH / SCREEN_HEIGHT;
rect.y = SCREEN_WIDTH / SCREEN_HEIGHT;
rect.w = SCREEN_WIDTH / 2;
rect.h = SCREEN_HEIGHT / 2;
SDL_Rect rect2;
rect2.x = 250;// / SCREEN_HEIGHT;
rect2.y = 250;// / SCREEN_HEIGHT;
rect2.w = SCREEN_WIDTH / 4;
rect2.h = SCREEN_HEIGHT / 4;
// Uint64 start = 0;
// Uint64 end = 0;
// float frameCount = 0;
// int count = 0;
while(!Exit) {
//start = SDL_GetPerformanceCounter();
SDL_Delay(16);
HandleInput();
SDL_SetRenderDrawColor(app.renderer, 0, 0, 255, 0);
SDL_RenderClear(app.renderer);
// if (count >= 100) {
// snprintf(buffer, sizeof(buffer), "FPS: %.2f", count / frameCount);
// count = 0;
// frameCount = 0;
// }
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0);
SDL_RenderFillRect(app.renderer, &(SDL_Rect){rect.x + XRel, rect.y + YRel, rect.w, rect.h});
SDL_SetRenderDrawColor(app.renderer, 128, 0, 0, 0);
SDL_RenderFillRect(app.renderer, &(SDL_Rect){rect2.x + XRel, rect2.y + YRel, rect2.w, rect2.h});
SDL_RenderPresent(app.renderer);
// end = SDL_GetPerformanceCounter();
// frameCount += (end - start) / (float)SDL_GetPerformanceFrequency();
// count++;
}
}
void HandleInput(void) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
Exit = 1;
break;
case SDL_MOUSEBUTTONDOWN:
MouseDown = 1;
break;
case SDL_MOUSEBUTTONUP:
MouseDown = 0;
break;
case SDL_MOUSEMOTION:
//if (MainMenu->HitCheck(event.motion.x, event.motion.y, &MainMenu->Area)) {
if (MouseDown){
XRel += event.motion.xrel;
YRel += event.motion.yrel;
}
case SDL_TEXTINPUT:
case SDL_KEYDOWN:
default:
break;
}
}
}