#include <iostream>
#include<stdio.h>
#include <SDL2/SDL.h>
#include<SDL2/SDL_ttf.h>
using namespace std;
SDL_Window* window = NULL;
SDL_Surface* surf=NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* txt=NULL;
TTF_Font* font=NULL;
bool done =false;
SDL_Color fg = { 243, 156, 18 };
void output(string text,int cx,int cy)
{
SDL_Rect srcRest;
SDL_Rect desRect;
surf = TTF_RenderText_Solid(font, text.c_str(), fg);
txt = SDL_CreateTextureFromSurface(renderer, surf);
TTF_SizeText(font, text.c_str(), &srcRest.w, &srcRest.h);
srcRest.x = 0;
srcRest.y = 0;
desRect.x = cx;
desRect.y = cy;
desRect.w = srcRest.w;
desRect.h = srcRest.h;
SDL_FreeSurface(surf);
SDL_RenderCopy(renderer, txt, &srcRest, &desRect);
}
void createRect(SDL_Rect &rect,int cx,int cy,int cw,int ch)
{
rect.x=cx;
rect.y=cy;
rect.w=cw;
rect.h=ch;
}
int generateRandomNumber()
{
return rand() % 100 + 1;
}
int string2Int(string st)
{
return stoi(st);
}
string int2String(int num)
{
return to_string(num);
}
int main(int argc, char *argv[])
{
string inputText="";
int secretNumber=generateRandomNumber();
bool isCalled=false,isPressed=false,save=false;
int point=100;
SDL_Event event;
if (SDL_Init(SDL_INIT_VIDEO) == 0) {
window= SDL_CreateWindow("GuessIt", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 500, SDL_WINDOW_SHOWN);
renderer=SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Rect rectInput,rectButton;
createRect(rectInput,168,100,65,65);
createRect(rectButton,150,250,100,40);
if( TTF_Init() == -1 ) cout<<"Error on TTF_init";
font = TTF_OpenFont("D:/FreeSansBold.ttf", 20);
if(font==NULL) cout<<"Font Location Error\n";
//SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_StartTextInput();
SDL_SetTextInputRect(&rectInput);
while (!done) {
SDL_SetRenderDrawColor(renderer,0,0,0,SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
done = true;
else if(event.type==SDL_MOUSEBUTTONDOWN)
{
int cx=event.button.x;
int cy=event.button.y;
if(cx>150 && cx<250 && cy>250 && cy<290)
{
isPressed=true;
isCalled=true;
save=true;
}
} else if(event.type==SDL_MOUSEBUTTONUP)
isPressed=false;
else if (event.key.keysym.sym == SDLK_BACKSPACE){
isCalled=false;
if(inputText.length()>0)
inputText.pop_back();
}
else if (event.type == SDL_TEXTINPUT)
inputText += event.text.text;
}
if(isCalled==true && inputText.length()!=0) {
int guessNumber=string2Int(inputText);
if (guessNumber > secretNumber) {
if(save){point--;save=false;}
output("Too Big!",160,350);
}
else if (guessNumber < secretNumber) {
if(save) {point--;save=false;}
output("Too Small!",160,350);
}
else{
if(save){point=100;save=false;}
output("You win!!!",160,350);
}
}
string tmp="POINT: "+int2String(point);
output(tmp,4,5);
SDL_SetRenderDrawColor(renderer,116,41,215,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &rectInput);
if(!isPressed){
SDL_SetRenderDrawColor(renderer,116,41,215,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &rectButton);
}
output("GuessIt",165,253);
surf = TTF_RenderText_Solid(font,inputText.c_str(),fg);
txt= SDL_CreateTextureFromSurface(renderer, surf);
SDL_FreeSurface(surf);
SDL_RenderCopy(renderer,txt,NULL,&rectInput);
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
}
if(renderer) SDL_DestroyRenderer(renderer);
if(window) SDL_DestroyWindow(window);
}
SDL_StopTextInput();
TTF_Quit();
TTF_CloseFont(font);
SDL_Quit();
return 0;
}
Above is my code. The program takes too much space though my Laptop has 8GB RAM, it took up more than 1500MB and then crashed down.
In my code, some texts are changed and frequently updated, but I know some methods are called many times. I tried to fix this but it didn’t work. Anyone can tell me how to do that?(Thanks in advance!)