First commit
This commit is contained in:
commit
b0030425a4
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/rust-version/
|
||||
/main
|
22
Makefile
Normal file
22
Makefile
Normal file
@ -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)
|
122
main.cpp
Normal file
122
main.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
#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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user