fide/main.cpp

190 lines
5.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>
#include <ext/rope>
using namespace __gnu_cxx;
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
};
SDL_Rect get_srcrect(char key) {
SDL_Rect srcrect;
srcrect.x = (key % 16) * 70;
srcrect.y = (key / 16) * 80;
srcrect.w = 80;
srcrect.h = 80;
return srcrect;
}
void render_char(char key, int row, int col, SDL_Renderer* renderer, SDL_Texture* font) {
SDL_Rect dstrect;
dstrect.x = col;
dstrect.y = row;
dstrect.w = 30;
dstrect.h = 30;
SDL_Rect srcrect = get_srcrect(key);
SDL_RenderCopy(renderer, font, &srcrect, &dstrect);
}
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;
} 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 = 100;
dstrect.h = 100;
// SDL_BlitSurface(imageSurface, &srcrect, screenSurface, &dstrect);
//Update the surface
// SDL_UpdateWindowSurface( window );
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;
float scale = 50;
rope<char> buffer;
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;
char mod = e.key.keysym.mod;
if (key == '\r') {
buffer.push_back('\n');
}
if (key == '-' && mod & KMOD_LCTRL) {
cout << "Minu" << endl;
}
if (key == '=' && mod & KMOD_LCTRL) {
cout << "Ploos" << endl;
}
if (key >= (char)'!' || key <= (char)'~') {
// keyPressed = key;
buffer.push_back(key);
}
if (key == SDLK_BACKSPACE) {
if (buffer.size() >= 2) {
buffer.pop_back();
buffer.pop_back();
}
}
cout << key << endl;
// switch (e.key.keysym.sym) {
// case SDLK_
// }
}
}
// SDL_FillRect( screenSurface, NULL, rgbMap);
SDL_RenderClear(renderer);
SDL_SetTextureColorMod(fontTexture, 0x0, 0, 0);
rope<char>::iterator it;
for (it = buffer.mutable_begin(); it != buffer.mutable_end(); it++) {
if (*it == '\n') {
rowCurr += 1;
colCurr = 0;
continue;
}
if (*it == '\r') {
continue;
}
if (*it >= (char)'!' || *it <= (char)'~') {
render_char(*it, rowCurr * 70, colCurr++ * 50, renderer, fontTexture);
}
}
colCurr = 0;
rowCurr = 0;
// Draw(keyPressed, renderer, fontTexture);
SDL_RenderPresent(renderer);
}
//Hack to get window to stay up
// SDL_FreeSurface(screenSurface);
SDL_FreeSurface(fontSurface);
SDL_DestroyTexture(fontTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}