Помогите плиз, этот код после запуска постепенно отжирает все ОЗУ.
Что может быть не так?
#include "stdafx.h"
#include
int state = 0;
bool showToBeDone = false;
void action(int i) {
if (i == 0) {
state = 3;
}
else if (i == 1) {
showToBeDone = true;
}
else {
state = 4;
}
}
bool XYInRect(const SDL_Rect& rect, int x, int y)
{
return ((x >= rect.x && x <= rect.x + rect.w) && (y >= rect.y && y <= rect.y + rect.h));
}
int main(int argc, char *argv[])
{
SDL_Rect prect;
std::string input = "";
const char* username = "Pavel";
prect.x = 300;
prect.y = 50;
bool isRunning = true;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
int active = 0;
SDL_Window* window = SDL_CreateWindow("Beeries 3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
SDL_Surface* screen = SDL_GetWindowSurface(window);
while (isRunning == true) {
SDL_FillRect(screen, NULL, 0x000000);
/*if (state == 0) {
SDL_Surface* hello = SDL_LoadBMP("startup.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(1000);
SDL_FreeSurface(hello);
hello = NULL;
state = 3;
}
else if (state == 3) {*/
SDL_Surface* fon = SDL_LoadBMP("fon.bmp");
SDL_BlitSurface(fon, NULL, screen, NULL);
int size = 0;
SDL_Surface* pivo = NULL;
TTF_Font* Sans = TTF_OpenFont("Roboto-Light.ttf", 24);
SDL_Color wht = { 0,0,0,0 };
SDL_Rect usernameRect;
SDL_Surface* usernamed = NULL;
pivo = SDL_LoadBMP("pivo.bmp");
// usernamed = TTF_RenderUTF8_Solid(Sans, username, wht);
usernameRect.x = prect.x;
usernameRect.y = prect.y + 150;
SDL_Surface* chat = SDL_LoadBMP("chat.bmp");
SDL_Surface* txt = NULL;
SDL_Rect chatRect;
chatRect.x = 50;
chatRect.y = 500;
SDL_Rect msgRect;
msgRect.x = 70;
msgRect.y = 550;
SDL_BlitSurface(chat, NULL, screen, &chatRect);
const char* etext = input.c_str();
txt = TTF_RenderText_Solid(Sans, etext, wht);
SDL_BlitSurface(txt, NULL, screen, &msgRect);
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_MOUSEBUTTONDOWN) {
int x;
int y;
SDL_GetMouseState(&x, &y);
if (y <= 200) {}
else if (XYInRect(chatRect, x, y)) {
}
else {
prect.x = x - 100;
prect.y = y - 100;
SDL_BlitSurface(pivo, NULL, screen, &prect);
SDL_UpdateWindowSurface(window);
}
}
else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.scancode != 21 && e.key.keysym.scancode != 42 && input.length() < 29) {
input += SDL_GetScancodeName(e.key.keysym.scancode);
std::cout << input << std::endl;
}
else if (e.key.keysym.scancode == 42) {
input = input.substr(0, input.size() - 1);
std::cout << input << std::endl;
}
else {
/* menu here */
}
}
}
SDL_BlitSurface(pivo, NULL, screen, &prect);
SDL_BlitSurface(usernamed, NULL, screen, &usernameRect);
SDL_UpdateWindowSurface(window);
/*}
else {
isRunning = false;
}
*/
}
SDL_Quit();
return 0;
}
Ответ
Не знаю как у вас устроен код, но должно быть хотяб так для начала.
SDL_Surface* fon = SDL_LoadBMP("fon.bmp");
SDL_Surface* pivo = SDL_LoadBMP("pivo.bmp");
SDL_Surface* usernamed = TTF_RenderUTF8_Solid(Sans, username, wht);
SDL_Surface* chat = SDL_LoadBMP("chat.bmp");
SDL_Surface* txt = NULL;
TTF_Font* Sans = TTF_OpenFont("Roboto-Light.ttf", 24);
int size = 0;
SDL_Color wht = { 0,0,0,0 };
SDL_Rect usernameRect;
while (isRunning == true) {
SDL_FillRect(screen, NULL, 0x000000);
SDL_BlitSurface(fon, NULL, screen, NULL);
size = 0;
//wht = { 0,0,0,0 };
usernameRect.x = prect.x;
usernameRect.y = prect.y + 150;
txt = NULL;
}
Также вам придётся освобождать память (думаю в конце функции main для вашего кода и до SDL_Quit();) при помощи:
SDL_FreeSurface(fon);
SDL_FreeSurface(pivo);
SDL_FreeSurface(usernamed);
SDL_FreeSurface(chat);
SDL_FreeSurface(txt);
TTF_CloseFont(Sans); // и ещё шрифт выгрузить.
SDL_Quit(); // выгрузить библиотеку
Комментариев нет:
Отправить комментарий