Compare commits

..

No commits in common. "9d725d0b2f6f889a4f26af96a9b830bf9fef125a" and "868922c68eb7d2dab6e412879517893ed5da7a2e" have entirely different histories.

3 changed files with 35 additions and 66 deletions

View File

@ -6,7 +6,7 @@ CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -Wall -Wpedantic
COMPILER_FLAGS = -w
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image

139
main.cpp
View File

@ -21,34 +21,69 @@ enum KeyPressSurfaces
KEY_PRESS_SURFACE_TOTAL
};
SDL_Rect get_srcrect(char key) {
SDL_Rect srcrect;
srcrect.x = (key % 16) * 70;
srcrect.y = (key / 16) * 80;
int main(int argc, char *argv[]) {
SDL_Window* window = NULL;
srcrect.w = 80;
srcrect.h = 80;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
return srcrect;
}
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_SetRenderDrawColor(renderer, 130, 163, 255, 1);
void render_char(char key, int row, int col, SDL_Renderer* renderer, SDL_Texture* font) {
SDL_Rect dstrect;
SDL_Surface* fontSurface = IMG_Load("font.png");
if (fontSurface == NULL) {
cout << "Could not find spritesheet" << endl;
return 1;
}
SDL_Texture* fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface);
// SDL_Surface* screenSurface = NULL;
// //Get window surface
// screenSurface = SDL_GetWindowSurface( window );
dstrect.x = col;
dstrect.y = row;
dstrect.w = 80;
dstrect.h = 80;
//Fill the surface white
// auto rgbMap = SDL_MapRGB( screenSurface->format, 0x82, 0xA3, 0xFF);
SDL_Rect srcrect = get_srcrect(key);
SDL_RenderCopy(renderer, font, &srcrect, &dstrect);
}
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);
void Draw(char keyPressed, SDL_Renderer* renderer, SDL_Texture* fontTexture) {
SDL_Rect srcrect;
SDL_Rect dstrect;
if (keyPressed > 0) {
srcrect.x = (keyPressed % 16) * 70;
srcrect.y = (keyPressed / 16) * 80;
@ -70,76 +105,10 @@ void Draw(char keyPressed, SDL_Renderer* renderer, SDL_Texture* fontTexture) {
//Update the surface
// SDL_UpdateWindowSurface( window );
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, fontTexture, &srcrect, &dstrect);
}
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_SetRenderDrawColor(renderer, 130, 163, 255, 1);
SDL_Surface* fontSurface = IMG_Load("output.png");
if (fontSurface == NULL) {
cout << "Could not find spritesheet" << endl;
return 1;
}
SDL_Texture* fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface);
// SDL_Surface* screenSurface = NULL;
// //Get window surface
// screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
// auto rgbMap = SDL_MapRGB( screenSurface->format, 0x82, 0xA3, 0xFF);
SDL_Event e;
bool quit = false;
int keyPressed = -1;
int rowCurr = 0;
int colCurr = 0;
string text = "Some text";
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_RenderClear(renderer);
SDL_SetTextureColorMod(fontTexture, 0xFF, 0, 0);
for (int i = 0; i < 10; i++) {
render_char(text[i], rowCurr, colCurr++ * 50, renderer, fontTexture);
}
colCurr = 0;
// Draw(keyPressed, renderer, fontTexture);
SDL_RenderPresent(renderer);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB