#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_SetRenderDrawColor(renderer, 130, 163, 255, 1); 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 ); //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 = 100; dstrect.h = 100; // SDL_BlitSurface(imageSurface, &srcrect, screenSurface, &dstrect); //Update the surface // SDL_UpdateWindowSurface( window ); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, fontTexture, &srcrect, &dstrect); 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; }