1. Help Wrapping TTF_Font for EuSDL[SOLVED]

Hello all,

I am trying to wrap SDL_TTF for EuSDL2. Everything seems to be going fine, except for this. The window closes right away after the command to open a font is issued.

EDIT: I forgot to put the "+" before the function name. However I am having trouble displaying the text correctly on the screen. Any help would be appericated.

--wrapper code 
public function TTF_Init() 
 
 return c_func(xTTF_Init,{}) 
	 
end function 
 
public constant xTTF_OpenFont = define_c_func(ttf,"+TTF_OpenFont",{C_POINTER,C_INT},C_POINTER), 
				xTTF_OpenFontIndex = define_c_func(ttf,"+TTF_OpenFontIndex",{C_POINTER,C_INT,C_LONG},C_POINTER), 
				xTTF_OpenFontRW = define_c_func(ttf,"+TTF_OpenFontRW",{C_POINTER,C_INT,C_INT},C_POINTER), 
				xTTF_OpenFontIndexRW = define_c_func(ttf,"+TTF_OpenFontIndexRW",{C_POINTER,C_INT,C_INT,C_LONG},C_POINTER) 
				 
public function TTF_OpenFontRW(atom src,atom xfree,atom size,atom idx) 
 
 return c_func(xTTF_OpenFontRW,{src,xfree,size,idx}) 
	 
end function 
 
public function TTF_OpenFontIndexRW(atom src,atom xfree,atom size,atom idx) 
 
 return c_func(xTTF_OpenFontIndexRW,{src,xfree,size,idx}) 
	 
end function 
				 
public function TTF_OpenFont(sequence file,integer size) 
 
 atom str = allocate_string(file,1) 
  
 return c_func(xTTF_OpenFont,{str,size}) 
	 
end function 
 
public function TTF_OpenFontIndex(sequence file,atom size,atom idx) 
 
 atom str = allocate_string(file,1) 
  
 return TTF_OpenFontIndexRW(SDL_RWFromFile(str,"rb"),1,10,1) 
	 
end function 
--Example 
without warning 
 
include std/machine.e 
 
include EuSDL2.ew 
include EuSDLTTF.ew 
 
include flags.e 
 
atom x = SDL_Init(SDL_INIT_VIDEO) 
 
atom win = SDL_CreateWindow("Font Ex",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN) 
atom ren = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED) 
 
atom running = 1 
atom key = 0 
 
 
atom f = TTF_Init() 
--f = TTF_OpenFont("arial.ttf",20) --cause window to close right away 
 
while running = 1 do 
	 
	SDL_PumpEvents() 
	 
	key = SDL_GetKeyboardState(key) 
	 
	if peek(key+SDL_SCANCODE_ESCAPE) > 0 then 
		running = 0 
	end if 
	 
	SDL_RenderClear(ren) 
	 
	SDL_RenderPresent(ren) 
 
end while 
 
SDL_DestroyRenderer(ren) 
SDL_DestroyWindow(win) 
 
SDL_Quit() 

I also tried this:

public function TTF_OpenFont(sequence file,integer size) 
 
 atom str = allocate_string(file,1) 
  
 return TTF_OpenFontRW(SDL_RWFromFile(str,"rb"),1,20,1) 
	 
end function 

When I try to use the TTF_OpenFont command, it gives me the error fname is 7638376 from SDL_RWFromFile (type_check failure error)

new topic     » topic index » view message » categorize

2. Re: Help Wrapping TTF_Font for EuSDL[SOLVED SORTA]

--wrapper code 
public constant xTTF_RenderText_Solid = define_c_func(ttf,"+TTF_RenderText_Solid",{C_POINTER,C_POINTER,C_UINT,C_UINT,C_UINT,C_UINT},C_POINTER) 
 
--SDLColor is a struct, so it is declared as 4 separate C_UINT's (r,g,b,u) u means unused 
public function TTF_RenderText_Solid(atom font,sequence text,atom r,atom g,atom b,atom u) 
 
 atom str = allocate_string(text,1) 
  
 return c_func(xTTF_RenderText_Solid,{font,str,r,g,b,u}) 
	 
end function 
--Hello world appears as one big font covering most of the screen 
 
without warning 
 
include std/machine.e 
 
include EuSDL2.ew 
include EuSDLTTF.ew 
 
include flags.e 
 
atom x = SDL_Init(SDL_INIT_VIDEO) 
 
atom win = SDL_CreateWindow("Font Example",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN) 
atom ren = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED) 
 
atom running = 1 
atom key = 0 
 
 
atom f = TTF_Init() 
 
if f = 1 then 
	puts(1,"Failed to init TTF!\n") 
	abort(0) 
end if 
 
f = TTF_OpenFont("arial.ttf",10)  
 
procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
	poke4(rect,sx) 
	poke4(rect+4,sy) 
	poke4(rect+8,tx) 
	poke4(rect+12,ty) 
	 
end procedure 
 
atom w = 0 
atom he = 0 
 
atom drect = allocate(10) 
 
calc_rect(drect,10,10,w,he) 
 
atom h = TTF_RenderText_Solid(f,"Hello World",255,255,0,0) 
atom ht = SDL_CreateTextureFromSurface(ren,h) 
 
SDL_QueryTexture(ht,0,0,w,he) 
 
while running = 1 do 
	 
	SDL_PumpEvents() 
	 
	key = SDL_GetKeyboardState(key) 
	 
	if peek(key+SDL_SCANCODE_ESCAPE) > 0 then 
		running = 0 
	end if 
	 
	SDL_RenderClear(ren) 
	 
	SDL_RenderCopy(ren,ht,0,0) --putting drect as the last parameter doesen't show anything, leaving it as 0 shows "hello world" 
	 
	SDL_RenderPresent(ren) 
 
end while 
 
SDL_DestroyRenderer(ren) 
SDL_DestroyWindow(win) 
 
TTF_CloseFont(f) 
 
SDL_DestroyTexture(ht) 
SDL_FreeSurface(h) 
 
TTF_Quit() 
 
SDL_Quit() 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Help Wrapping TTF_Font for EuSDL[SOLVED SORTA]

Did you look at EuSDL2_ttf ? https://archive.usingeuphoria.com/eusdl2_ttf_v0.1.zip

Maybe it can help.

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

4. Re: Help Wrapping TTF_Font for EuSDL[SOLVED]

jmduro said...

Did you look at EuSDL2_ttf ? https://archive.usingeuphoria.com/eusdl2_ttf_v0.1.zip

Maybe it can help.

Jean-Marc

After look through their source code, I was able to adapt my example and got it to work. Thanks!

without warning 
 
include std/machine.e 
 
include EuSDL2.ew 
include EuSDLTTF.ew 
 
include flags.e 
 
atom x = SDL_Init(SDL_INIT_VIDEO) 
 
atom win = SDL_CreateWindow("Font Example",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN) 
atom ren = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED) 
 
atom running = 1 
atom key = 0 
 
 
atom f = TTF_Init() 
 
if f = 1 then 
	puts(1,"Failed to init TTF!\n") 
	abort(0) 
end if 
 
f = TTF_OpenFont("arial.ttf",20)  
 
procedure calc_rect(atom rect,integer sx,integer sy,integer tx,integer ty) 
 
	poke4(rect,sx) 
	poke4(rect+4,sy) 
	poke4(rect+8,tx) 
	poke4(rect+12,ty) 
	 
end procedure 
 
atom h = TTF_RenderText_Solid(f,"Hello World",255,255,0,0) 
atom ht = SDL_CreateTextureFromSurface(ren,h) 
 
function renderTexture(atom tex,atom ren,atom x,atom y) 
 
 atom r = allocate(16) 
 poke4(r,{x,y,0,0}) 
 SDL_QueryTexture(tex,0,0,r+8,r+12) 
 return r 
	 
end function 
 
atom r = renderTexture(ht,ren,0,0) 
atom w = peek4u(r+8) 
atom ih = peek4u(r+12) 
atom ix = floor((640 / 2) - (w/2)) 
atom y = floor((480 / 2) - (ih/2)) 
poke4(r,{ix,y}) 
 
while running = 1 do 
	 
	SDL_PumpEvents() 
	 
	key = SDL_GetKeyboardState(key) 
	 
	if peek(key+SDL_SCANCODE_ESCAPE) > 0 then 
		running = 0 
	end if 
	 
	SDL_RenderClear(ren) 
	 
	SDL_RenderCopy(ren,ht,0,r) 
	 
	SDL_RenderPresent(ren) 
 
end while 
 
SDL_DestroyRenderer(ren) 
SDL_DestroyWindow(win) 
 
TTF_CloseFont(f) 
 
SDL_DestroyTexture(ht) 
SDL_FreeSurface(h) 
 
TTF_Quit() 
 
SDL_Quit() 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu