Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL/SDL.h>
- #include <SDL_ttf.h>
- #include <string>
- SDL_Event event;
- SDL_Surface* screen;
- SDL_Surface* message;
- SDL_Surface* background;
- TTF_Font *font = NULL;
- SDL_Color textColor = {255, 255, 255};
- SDL_Surface* load_image(std::string filename)
- {
- SDL_Surface* loadedImage = NULL;
- SDL_Surface* optimizedImage = NULL;
- loadedImage = SDL_LoadBMP(filename.c_str());
- if (loadedImage != NULL)
- {
- optimizedImage = SDL_DisplayFormat(loadedImage);
- SDL_FreeSurface(loadedImage);
- }
- return optimizedImage;
- }
- void apply_surface (int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
- {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, clip, destination, &offset);
- }
- class StringInput
- {
- private:
- std::string str;
- SDL_Surface* text;
- public:
- StringInput();
- ~StringInput();
- void handle_input();
- void show_centered();
- };
- StringInput::StringInput()
- {
- str = "";
- text = NULL;
- SDL_EnableUNICODE(SDL_ENABLE);
- }
- StringInput::~StringInput()
- {
- SDL_FreeSurface(text);
- SDL_EnableUNICODE(SDL_DISABLE);
- }
- void StringInput::handle_input()
- {
- if(event.type == SDL_KEYDOWN)
- {
- std::string temp = str;
- if(str.length() <= 16)
- {
- if(event.key.keysym.unicode == (Uint16)' ')
- {
- str += (char)event.key.keysym.unicode;
- }
- if((event.key.keysym.unicode >= (Uint16)'0') && (event.key.keysym.unicode <= (Uint16)'9'))
- {
- str += (char)event.key.keysym.unicode;
- }
- if((event.key.keysym.unicode >= (Uint16)'A') && (event.key.keysym.unicode <= (Uint16)'Z'))
- {
- str += (char)event.key.keysym.unicode;
- }
- if((event.key.keysym.unicode >= (Uint16)'a') && (event.key.keysym.unicode <= (Uint16)'z'))
- {
- str += (char)event.key.keysym.unicode;
- }
- if((event.key.keysym.unicode == SDLK_BACKSPACE) && (str.length() != 0))
- {
- str.erase(str.length() - 1);
- }
- if(str != temp)
- {
- SDL_FreeSurface(text);
- text = TTF_RenderText_Solid(font, str.c_str(), textColor);
- }
- }
- }
- }
- void StringInput::show_centered()
- {
- if(text != NULL)
- {
- apply_surface((640 - text->w) / 2, (480 - text->h) / 2, text, screen);
- }
- }
- int main(int, char**)
- {
- bool quit = false;
- bool nameEntered = false;
- StringInput name;
- TTF_Init();
- SDL_Init(SDL_INIT_EVERYTHING);
- screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
- font = TTF_OpenFont("lazy.ttf", 28);
- message = TTF_RenderText_Solid(font, "Nouveau record ! Entrez votre nom :", textColor);
- background = load_image("background.bmp");
- //SDL_EnableUNICODE(SDL_ENABLE);
- while(quit == false)
- {
- while(SDL_PollEvent(&event))
- {
- if(nameEntered == false)
- {
- name.handle_input();
- if((event.type == SDL_KEYDOWN) && (event.key.keysym.sym == SDLK_RETURN))
- {
- //SDL_EnableUNICODE(SDL_DISABLE);
- nameEntered = true;
- SDL_FreeSurface(message);
- message = TTF_RenderText_Solid(font, "Rang : 1er", textColor);
- }
- }
- if(event.type == SDL_QUIT)
- {
- quit = true;
- }
- }
- apply_surface(0, 0, background, screen);
- apply_surface((640 - message->w) / 2, ((480 / 2) - message->h) / 2, message, screen);
- name.show_centered();
- SDL_Flip(screen);
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement