commit b0030425a498caf0f7ab53ac50eb677325e17002 Author: Joseph Ferano Date: Sun Sep 17 21:27:02 2023 +0700 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..034db26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/rust-version/ +/main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..007152e --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +#OBJS specifies which files to compile as part of the project +OBJS = main.cpp + +#CC specifies which compiler we're using +CC = g++ + +#COMPILER_FLAGS specifies the additional compilation options we're using +# -w suppresses all warnings +COMPILER_FLAGS = -w + +#LINKER_FLAGS specifies the libraries we're linking against +LINKER_FLAGS = -lSDL2 -lSDL2_image + +#OBJ_NAME specifies the name of our exectuable +OBJ_NAME = main + +run: all + ./$(OBJ_NAME) + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) diff --git a/font.png b/font.png new file mode 100644 index 0000000..c51c788 Binary files /dev/null and b/font.png differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0b79081 --- /dev/null +++ b/main.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include + +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; +}