Autor Wątek: Wyciek pamięci sdl 2.0 TTF  (Przeczytany 1470 razy)

Offline jegor377

  • Użytkownik

# Grudzień 29, 2014, 18:24:37
Od jakiegoś czasu piszę sobie grę i napotkałem na pewien problem, mianowicie, przy każdym wyświetleniu tekstu mój program zamiast usunąć dane to rośnie i rośnie i nic nie da się z nim zrobić, a przecież zrobiłem konstruktor itp... a tu dupa program rośnie i ma mnie w swojej waginie :(

Wcześniej korzystałem z klasy, ale ona rosła, więc wpadłem na pomysł, że zamiast tworzyć obiekt i go w kółko wyświetlać to wykorzystam do tego funkcję, która wyświetli dane, a potem zaraz się usunie, ale to nie pomogło, program dalej rósł i rósł... Może wy coś wymyślicie, bo ja już się poddaję, poniżej daję kod do programu i tej funkcji...

main.cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include "source.h"
#include <vector>
#undef main

using namespace std;

int xmap=20, ymap=12, xmouse, ymouse;
int fps, frames;
unsigned long long int fpsDelay=SDL_GetTicks();
bool isSelected=false;

string licznik, mouseposition;

int main(int argc, char * argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window * game_window = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    SDL_Renderer * game_render = SDL_CreateRenderer(game_window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Event * game_events = new SDL_Event();

    vector <GameObject> wojsko;
    vector <GameObject> wrogowie;

    GameObject createbutton(8, 0, ymap, 192, 96, 0, 0, 128, 64, 0.0);

    GameObject * stage[6];

    stage[0] = new GameObject(5, 1, 0, 32, 32, 0, 0, 64, 64, 0.0);
    stage[1] = new GameObject(5, 1, 1, 32, 32, 0, 0, 64, 64, 0.0);
    stage[2] = new GameObject(5, 0, 1, 32, 32, 0, 0, 64, 64, 0.0);
    stage[3] = new GameObject(5, xmap-2, ymap-1, 32, 32, 0, 0, 64, 64, 0.0);
    stage[4] = new GameObject(5, xmap-2, ymap-2, 32, 32, 0, 0, 64, 64, 0.0);
    stage[5] = new GameObject(5, xmap-1, ymap-2, 32, 32, 0, 0, 64, 64, 0.0);

    CSprite * tekstura[9];

    GameObject ** grass = new GameObject * [xmap];
    for(int a=0; a<=xmap-1; a++) grass[a] = new GameObject [ymap];

    GameObject mousepointer(2, 0, 0, 32, 32, 0, 0, 64, 64, 0.0, false, 0, 0, 0, 0, 0, 0, 0);
    GameObject swoj(3, 0, 0, 32, 32, 0, 0, 64, 64, 0.0, false, 0, 0, 0, 0, 0, 0, 0);
    GameObject wrog(4, xmap-1, ymap-1, 32, 32, 0, 0, 64, 64, 0.0, false, 0, 0, 0, 0, 0, 0, 0);

    tekstura[0] = new CSprite("grass.png", game_render);
    tekstura[1] = new CSprite("anim.png", game_render);
    tekstura[2] = new CSprite("zaznaczenie.png", game_render);
    tekstura[3] = new CSprite("swoj.png", game_render);
    tekstura[4] = new CSprite("wrog.png", game_render);
    tekstura[5] = new CSprite("stage.png", game_render);
    tekstura[6] = new CSprite("button.png", game_render);
    tekstura[7] = new CSprite("button2.png", game_render);
    tekstura[8] = new CSprite("button3.png", game_render);

    for(int y=0; y<=ymap-1; y++)
        for(int x=0; x<=xmap-1; x++) grass[x][y] = GameObject(0, x, y, 32, 32, 0, 0, 64, 64, 0.0, false, 0, 0, 0, 0, 0, 0, 0);

    //Text Mousepos("test", game_render, 20, "Tahoma.ttf", 0, 0, 255);
    //Text textbutton("CREATE", game_render, 30, "Tahoma.ttf", 255, 255, 255);
    //Text fpsshowing("test", game_render, 20, "Tahoma.ttf", 0, 0, 255);

    //ShowText("Create", (createbutton.xpos+1.4)*32, (createbutton.ypos+0.9)*32, 30, "Tahoma.ttf", 255, 255, 255, game_render);

    while(game_events->type!=SDL_QUIT && game_events->type!=SDL_KEYDOWN)
    {
        SDL_PollEvent(game_events);
        SDL_RenderClear(game_render);
        ShowFPS(&fps, &fpsDelay, &frames);
        for(int y=0; y<=ymap-1; y++)
        {
            for(int x=0; x<=xmap-1; x++)
            {
                tekstura[0]->Draw(&grass[x][y]);
            }
        }
        MouseController(game_events, &xmouse, &ymouse, xmap, ymap, &mousepointer, &isSelected, &createbutton);

        for(int x=0; x<=5; x++) tekstura[stage[x]->type]->Draw(stage[x]);
        if(isSelected==true) tekstura[mousepointer.type]->Draw(&mousepointer);
        tekstura[createbutton.type]->Draw(&createbutton);
        //fpsshowing.Draw("FPS : "+intToStr(frames), 20, 10);


        tekstura[swoj.type]->Draw(&swoj);
        tekstura[wrog.type]->Draw(&wrog);
        TextOn("test",0, 0, 20, "Tahoma.ttf", 0,0,0, game_render);

        //TextOn("FPS : "+intToStr(frames), 20, 10, 20, "Tahoma.ttf", 0, 0, 255, game_render);
        //textbutton.Draw("Create", (createbutton.xpos+1.4)*32, (createbutton.ypos+0.9)*32);
        //ShowText("FPS : "+intToStr(frames), 20, 10, 20, "Tahoma.ttf", 0, 255, 0, game_render);
        //Mousepos.Draw("Mouse Position: x = "+intToStr(xmouse/32)+" y = "+intToStr(ymouse/32), 20, 40);
        //ShowText("Mouse Position: x = "+intToStr(xmouse/32)+" y = "+intToStr(ymouse/32), 20, 40, 20, "Tahoma.ttf", 0, 255, 0, game_render);
        SDL_RenderPresent(game_render);
    }
    SDL_DestroyRenderer(game_render);
    SDL_DestroyWindow(game_window);
    delete game_events;
    SDL_Quit();
    return 0;
}

source.h
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>

using namespace std;

class GameObject
{
public:

    int type;
    int xpos, ypos, xsize, ysize, xcpos, ycpos, xcsize, ycsize;
    int ximagesize, yimagesize, animstart, animend, animrow, currentframe, animspeed;
    unsigned long long int animDelay;
    double angle;
    bool animation;
    SDL_Rect pos, cut;

               //type  xpos   ypos  xsize  ysize  xcpos  ycpos  xcsize ycsize  angle       animation   xis    yis     as     ae     ar     asp     cf
    GameObject(int=0, int=0, int=0, int=0, int=0, int=0, int=0, int=0, int=0, double=0.0, bool=false, int=0, int=0, int=0, int=0, int=0, int=0, int=0);
};

class CSprite
{
public:

    const char * name;

    SDL_Texture * image;
    SDL_Renderer * render;
    SDL_RendererFlip flip;

    SDL_Rect pos, cut;

    double angle;

    CSprite(const char * n=NULL, SDL_Renderer * r=NULL);
    ~CSprite();

    void Draw(GameObject * object);
    void PlayAnimation(GameObject * animate_object);
};

class Text
{
public:

    string text;
    int fontsize;
    const char * fontname;
    int w, h;

    TTF_Font * font;
    SDL_Surface * message;
    SDL_Texture * textmessage;
    SDL_Renderer * render;
    SDL_Rect pos;
    SDL_Color color;

    Text(string=NULL, SDL_Renderer * ren=NULL, int=0, const char * f=NULL, int=255, int=255, int=255);
    ~Text();

    void Draw(string t, int xp, int yp);
};

void ShowFPS(int * fps, unsigned long long int * fpsDelay, int * frames);

string intToStr(int n);

//const char * intToC_Char(int convert);

void MouseController(SDL_Event * zdarzenie, int *xm, int *ym, int xmap, int ymap, GameObject * object, bool * isSelected, GameObject * button);

void ShowText(string textmessage, int xpos, int ypos, int fontsize, const char * fontname, int r, int b, int g, SDL_Renderer * render);

void TextOn(string textm, int xp, int yp, int fontsize, const char * fontname, int r, int g, int b, SDL_Renderer * render);

source.cpp
#include "source.h"

using namespace std;

GameObject::GameObject(int t, int xp, int yp, int xs, int ys, int xcp, int ycp, int xcs, int ycs, double a, bool anim, int xis, int yis, int as, int ae, int ar, int asp, int cf)
{
    type=t;
    xpos=xp;
    ypos=yp;
    xsize=xs;
    ysize=ys;
    xcpos=xcp;
    ycpos=ycp;
    xcsize=xcs;
    ycsize=ycs;
    angle=a;
    animation=anim;
    ximagesize=xis;
    yimagesize=yis;

    pos.x=xpos*32;
    pos.y=xpos*32;
    pos.w=xsize;
    pos.h=ysize;
    cut.x=xcpos;
    cut.y=ycpos;
    cut.w=xcsize;
    cut.h=ycsize;

    animDelay=SDL_GetTicks();
}

CSprite::CSprite(const char * n, SDL_Renderer * r)
{
    name=n;
    render=r;
    if(name!=NULL && render!=NULL)
    {
        image=IMG_LoadTexture(render, name);
        if(image==NULL) cout<<"[CSprite->Konstruktor] Nie udało się załadować tekstury do pamięci."<<endl;
    }
    else
    {
        cout<<"NIE PODANO ARGUMENTÓW!!"<<endl;
        if(n==NULL) cout<<"name == NULL"<<endl;
        else cout<<"name != NULL"<<endl;
        if(r==NULL) cout<<"render == NULL"<<endl;
        else cout<<"render != NULL"<<endl;
    }
    flip=SDL_FLIP_NONE;
}

CSprite::~CSprite()
{
    SDL_DestroyTexture(image);
}

void CSprite::Draw(GameObject * object)
{
    object->pos.x=object->xpos*32;
    object->pos.y=object->ypos*32;
    pos=object->pos;
    angle=object->angle;
    if(object->animation==true) PlayAnimation(object);
    else cut=object->cut;
    SDL_RenderCopyEx(render, image, &cut, &pos, angle, NULL, flip);
}

void CSprite::PlayAnimation(GameObject * animate_object)
{
    if(animate_object->animDelay+animate_object->animspeed<SDL_GetTicks())
    {
        if(animate_object->animend<=animate_object->currentframe) animate_object->currentframe=animate_object->animstart;
        else animate_object->currentframe=animate_object->currentframe+1;
        cut.x=animate_object->currentframe*(animate_object->xcsize/animate_object->ximagesize);
        cut.y=animate_object->animrow*(animate_object->ycsize/animate_object->yimagesize);
        cut.w=(animate_object->xcsize/animate_object->ximagesize);
        cut.h=(animate_object->ycsize/animate_object->yimagesize);
        animate_object->animDelay=SDL_GetTicks();
    }
}

Text::Text(string t, SDL_Renderer * ren, int fs, const char * f, int r, int g, int b)
{
    fontname=f;
    fontsize=fs;
    text=t;
    font = TTF_OpenFont(fontname, fontsize);
    if(font==NULL) cout<<"[Text->Konstruktor] Nie udało się otworzyć czcionki."<<endl;
    render=ren;
    if(render==NULL) cout<<"[Text->Konstrutkor] Brak rendera!."<<endl;
    message=NULL;
    textmessage=NULL;
    color.r=r;
    color.g=g;
    color.b=b;
    w=0;
    h=0;
    message = TTF_RenderText_Solid(font, text.c_str(), color);
    textmessage = SDL_CreateTextureFromSurface(render, message);
    SDL_FreeSurface(message);
}

Text::~Text()
{
    SDL_DestroyTexture(textmessage);
    //SDL_FreeSurface(message);
    TTF_CloseFont( font );
}

void Text::Draw(string t, int xp, int yp)
{
    pos.x=xp;
    pos.y=yp;
    message = TTF_RenderText_Solid(font, text.c_str(), color);
    textmessage = SDL_CreateTextureFromSurface(render, message);
    SDL_QueryTexture(textmessage, NULL, NULL, &w, &h);
    pos.w=w;
    pos.h=h;
    text=t;
    SDL_FreeSurface(message);
    SDL_RenderCopy(render, textmessage, NULL, &pos);
    textmessage=NULL;
    message=NULL;
}
/*
void Text::SetValues(const char * f, int sizeofFont, string t)
{
    text=t;
    fontsize=sizeofFont;
    fontname=f;
    font = TTF_OpenFont(fontname, fontsize);
    w=0;
    h=0;
    message = TTF_RenderText_Solid(font, text.c_str(), color);
    textmessage = SDL_CreateTextureFromSurface(render, message);
    SDL_QueryTexture(textmessage, NULL, NULL, &w, &h);
}*/
/*
void Text::SetText(const char * t)
{
    text=t;
}*/

void ShowFPS(int * fps, unsigned long long int * fpsDelay, int * frames)
{
    *fps=*fps+1;
    if(*fpsDelay+1000<=SDL_GetTicks())
    {
        *fpsDelay=SDL_GetTicks();
        *frames=*fps;
        *fps=0;
    }
}

string intToStr(int n)
{
     string tmp, ret;
     if(n < 0) {
      ret = "-";
      n = -n;
     }
     do {
      tmp += n % 10 + 48;
      n -= n % 10;
     }
     while(n /= 10);
     for(int i = tmp.size()-1; i >= 0; i--)
      ret += tmp[i];
     return ret;
}

void MouseController(SDL_Event * zdarzenie, int *xm, int *ym, int xmap, int ymap, GameObject * object, bool * isSelected, GameObject * button)
{
    SDL_GetMouseState(xm, ym);
    if(*ym/32<=ymap-1)
    {
        *isSelected=true;
    }
    else
    {
        *isSelected=false;
    }
    object->xpos=*xm/32;
    object->ypos=*ym/32;
    /*if(*xm/32>=0 && *xm/32<=5 && *ym/32>=12 && *ym<=14) button->type=6;
    else button->type=8;
    switch(zdarzenie->type)
    {
    case SDL_MOUSEBUTTONDOWN:
        if(zdarzenie->button.button==SDL_BUTTON_LEFT)
        {
            button->type=8;
        }
        break;

    case SDL_MOUSEBUTTONUP:
        if(zdarzenie->button.button==SDL_BUTTON_LEFT)
        {
            button->type=6;
        }
        break;
    }*/
}

void ShowText(string textmessage, int xpos, int ypos, int fontsize, const char * fontname, int r, int b, int g, SDL_Renderer * render)
{
    Text tekst(textmessage, render, fontsize, fontname, r, g, b);
    tekst.Draw(textmessage, xpos, ypos);
    //cout<<textmessage<<endl;
}

void TextOn(string textm, int xp, int yp, int fontsize, const char * fontname, int r, int g, int b, SDL_Renderer * render)
{
    TTF_Init();
    SDL_Renderer * renderdwa = render;
    TTF_Font * font = TTF_OpenFont(fontname, fontsize);
    if(font==NULL) cout<<"FONT == NULL"<<endl;
    //if(render==NULL) cout<<"RENDER == NULL"<<endl;
    SDL_Color kolor;
    kolor.r=r;
    kolor.g=g;
    kolor.b=b;
    SDL_Surface * wiadomos = TTF_RenderText_Solid(font, textm.c_str(), kolor);
    int w=0, h=0;
    SDL_Texture * tekstura = SDL_CreateTextureFromSurface(renderdwa, wiadomos);
    SDL_QueryTexture(tekstura, NULL, NULL, &w, &h);
    SDL_Rect pos;
    pos.x=xp;
    pos.y=yp;
    pos.w=w;
    pos.h=h;
    SDL_RenderCopy(render, tekstura, NULL, &pos);
    SDL_FreeSurface(wiadomos);
    tekstura=NULL;
    wiadomos=NULL;
    SDL_DestroyTexture(tekstura);
    TTF_CloseFont( font );
    TTF_Quit();
}

Ta funkcja

void TextOn(string textm, int xp, int yp, int fontsize, const char * fontname, int r, int g, int b, SDL_Renderer * render)
{
    TTF_Init();
    SDL_Renderer * renderdwa = render;
    TTF_Font * font = TTF_OpenFont(fontname, fontsize);
    if(font==NULL) cout<<"FONT == NULL"<<endl;
    //if(render==NULL) cout<<"RENDER == NULL"<<endl;
    SDL_Color kolor;
    kolor.r=r;
    kolor.g=g;
    kolor.b=b;
    SDL_Surface * wiadomos = TTF_RenderText_Solid(font, textm.c_str(), kolor);
    int w=0, h=0;
    SDL_Texture * tekstura = SDL_CreateTextureFromSurface(renderdwa, wiadomos);
    SDL_QueryTexture(tekstura, NULL, NULL, &w, &h);
    SDL_Rect pos;
    pos.x=xp;
    pos.y=yp;
    pos.w=w;
    pos.h=h;
    SDL_RenderCopy(render, tekstura, NULL, &pos);
    SDL_FreeSurface(wiadomos);
    tekstura=NULL;
    wiadomos=NULL;
    SDL_DestroyTexture(tekstura);
    TTF_CloseFont( font );
    TTF_Quit();
}

Offline Mr. Spam

  • Miłośnik przetworów mięsnych

Offline Xirdus

  • Redaktor

  • +3
# Grudzień 29, 2014, 19:30:35
    SDL_FreeSurface(wiadomos);
    tekstura=NULL;
    wiadomos=NULL;
    SDL_DestroyTexture(tekstura);
Paczaj i zastanawiaj.