fide/main.cpp
2023-09-17 21:27:02 +07:00

123 lines
3.1 KiB
C++

#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <stdio.h>
#include <iostream>
using namespace std;
//Screen dimension constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
int main(int argc, char *argv[]) {
SDL_Window* window = NULL;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow( "SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN );
if (window == NULL) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
SDL_Renderer* renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface* imageSurface = IMG_Load("font.png");
if (imageSurface == NULL) {
cout << "Could not find spritesheet" << endl;
return 1;
}
// SDL_Surface* screenSurface = NULL;
// //Get window surface
// screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
// auto rgbMap = SDL_MapRGB( screenSurface->format, 0x82, 0xA3, 0xFF);
int test = 0;
SDL_Event e;
bool quit = false;
int keyPressed = -1;
while (quit == false) {
while (SDL_PollEvent( &e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_KEYDOWN) {
char key = e.key.keysym.sym;
if (key >= (char)'!' || key <= (char)'~') {
keyPressed = key;
}
// switch (e.key.keysym.sym) {
// case SDLK_
// }
}
}
// SDL_FillRect( screenSurface, NULL, rgbMap);
SDL_Rect srcrect;
SDL_Rect dstrect;
if (keyPressed > 0) {
srcrect.x = (keyPressed % 16) * 70;
srcrect.y = (keyPressed / 16) * 80;
} else {
srcrect.x = 0;
srcrect.y = 0;
}
srcrect.w = 80;
srcrect.h = 80;
dstrect.x = SCREEN_WIDTH/2 - 40;
dstrect.y = SCREEN_HEIGHT/2 - 40;
dstrect.w = 10;
dstrect.h = 10;
// SDL_BlitSurface(imageSurface, &srcrect, screenSurface, &dstrect);
//Update the surface
// SDL_UpdateWindowSurface( window );
SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
//Hack to get window to stay up
// SDL_FreeSurface(screenSurface);
SDL_FreeSurface(imageSurface);
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}