🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

exception in sdl2_ttf.lib

Started by
5 comments, last by tkialwan 4 years, 2 months ago

when I am trying to render multi lines of text I get this exception ("Exception thrown at 0x00000000710020BA (SDL2_ttf.dll) in Project16-test record.exe: 0xC0000005: Access violation reading location 0x0000000000000008. occurred")

and this is my code:

#include "Ltext.h"

TTF_Font *gFont = TTF_OpenFont("carson.ttf", 24);

SDL_Color gTextColor = { 0, 0, 0, 0xFF };

Ltext::Ltext()

{

texy = 0;

texw = 0;

texh = 0;

mTexture = NULL;

textSurface = NULL;

gRecordingDeviceCount = 0;

}

Ltext::~Ltext()

{

}

void Ltext::loadtext(const char* text, SDL_Color textColor,SDL_Renderer* gren)

{

free();

SDL_SetRenderDrawColor(gren, 255, 255, 255, 255);

SDL_RenderClear(mrender);

//Render text surface

textSurface = TTF_RenderText_Solid(gFont, text, textColor);

if(textSurface != NULL)

{

//Create texture from surface pixels

mTexture = SDL_CreateTextureFromSurface(gren, textSurface);

if (mTexture == NULL)

{

printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());

}

//Get rid of old surface

SDL_FreeSurface(textSurface);

}

else

{

printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());

}

int x = 0;

SDL_QueryTexture(mTexture, NULL, NULL, &texw, &texh);

SDL_Rect dest = { 0,texy,texw,texh };

SDL_RenderCopy(gren, mTexture, NULL, &dest);

SDL_RenderPresent(gren);

texy = texy + 25;

}

void Ltext::free()

{

//Free texture if it exists

if (mTexture != NULL)

{

SDL_DestroyTexture(mTexture);

mTexture = NULL;

}

}

None

Advertisement

#pragma once

#include "stdfx.h"

class Ltext

{

public:

Ltext();

~Ltext();

void loadtext(const char* text, SDL_Color textColor,SDL_Renderer* gren);

void free();

int gRecordingDeviceCount;

int texy, texw, texh;

SDL_Renderer* mrender;

SDL_Surface* textSurface;

private:

SDL_Texture* mTexture;

TTF_Font *gFont;

SDL_Event e;

};

None

#include "Lrecord.h"

Ltext tex1;

SDL_Color mTextColor = { 0, 0, 0, 0xFF };

Lrecord::Lrecord()

{

mrender = NULL;

}

Lrecord::~Lrecord()

{

}

int Lrecord::select_mic()

{

bool qa1 = false;

SDL_Event e;

int index = 0;

SDL_Window* mic_win = SDL_CreateWindow("select mic", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);

if (mic_win == NULL)

printf("window error... %s", SDL_GetError());

mrender = SDL_CreateRenderer(mic_win, -1, SDL_RENDERER_ACCELERATED);

if (mrender == NULL)

printf("renderer error %s", SDL_GetError());

if (!qa1)

{

tex1.loadtext("Please select your phone from list by press it's number: ", mTextColor,mrender);

//Get capture device count

tex1.gRecordingDeviceCount = SDL_GetNumAudioDevices(SDL_TRUE);

//No recording devices

if (tex1.gRecordingDeviceCount < 1)

{

printf("Unable to get audio capture device! SDL Error: %s\n", SDL_GetError());

}

//At least one device connected

else

{

//Cap recording device count

if (tex1.gRecordingDeviceCount > MAX_RECORDING_DEVICES)

{

tex1.gRecordingDeviceCount = MAX_RECORDING_DEVICES;

}

//Render device names

std::stringstream promptText;

for (int i = 0; i < tex1.gRecordingDeviceCount; ++i)

{

//Get capture device name

promptText.str("");

promptText << i << ": " << SDL_GetAudioDeviceName(i, SDL_TRUE);

//Set texture from name

tex1.loadtext(promptText.str().c_str(), mTextColor,mrender);

}

}

if (SDL_PollEvent(&e) != 0)

{

if (e.type == SDL_KEYDOWN)

{

//Handle key press from 0 to 9

if (e.key.keysym.sym >= SDLK_0 && e.key.keysym.sym <= SDLK_9)

{

//Get selection index

index = e.key.keysym.sym - SDLK_0;

}

}

}

}

//Clear screen

return index;

}

void Lrecord::reco_win()

{

}

None

#pragma once

#include "stdfx.h"

#include "Ltext.h"

//Maximum number of supported recording devices

const int MAX_RECORDING_DEVICES = 10;

//Maximum recording time

const int MAX_RECORDING_SECONDS = 5;

//Maximum recording time plus padding

const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;

class Lrecord

{

public:

Lrecord();

~Lrecord();

int select_mic();

void reco_win();

//Renders texture at given point

SDL_Renderer* mrender;

private:

};

None

#pragma once

#include "stdfx.h"

#include "Ltext.h"

//Maximum number of supported recording devices

const int MAX_RECORDING_DEVICES = 10;

//Maximum recording time

const int MAX_RECORDING_SECONDS = 5;

//Maximum recording time plus padding

const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;

class Lrecord

{

public:

Lrecord();

~Lrecord();

int select_mic();

void reco_win();

//Renders texture at given point

SDL_Renderer* mrender;

private:

};

None

#include "stdfx.h"

#include "Lrecord.h"

int main(int argv, char* args[])

{

bool quit = false;

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)

{

printf("initialize failed...");

}

else

{

if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))

{

printf("Warning: Linear texture filtering not enabled!");

}

}

if (TTF_Init() == -1)

{

printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());

}

Lrecord *lr = new Lrecord();

lr->select_mic();

while (!quit)

{

}

return 0;

}

None

this is all code of my program exception thrown at the first block in textSurface (ttf_rendertext_solid)

None

This topic is closed to new replies.

Advertisement