Fixed some little errors with the drawing functions.
[luagame.git] / funcs_font.cpp
blob03a18752bf5af9752083c3eaae460e8d6e02f9b3
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include <map>
8 #include <string>
9 #include <lua.hpp>
10 #include <SDL/SDL.h>
11 #include <SDL/SDL_ttf.h>
12 #include "funcs_font.h"
13 #include "funcs_video.h"
14 #include "gl_surface.h"
16 //loads a font and returns a pointer to it
17 int l_load_font(lua_State *L){
18 TTF_Font *font;
19 font = TTF_OpenFont(luaL_checkstring(L,1), (int)lua_tonumber(L,2));
21 lua_pushlightuserdata(L,font);
22 return 1;
25 //unloads a font
26 int l_close_font(lua_State *L){
27 TTF_CloseFont((TTF_Font*)lua_touserdata(L,1));
28 lua_pushnil(L);
29 return 1;
32 //gets dimensions of a string as rendered with a font
33 int l_font_string_metrics(lua_State *L){
34 int w, h;
35 TTF_SizeUTF8((TTF_Font*)lua_touserdata(L,1), luaL_checkstring(L,2), &w, &h);
37 lua_pushinteger(L, w);
38 lua_pushinteger(L, h);
40 return 2;
43 //render a string in a font
44 int l_render_string(lua_State *L){
45 SDL_Surface *temp;
46 SDL_Color color;
48 color.r = (int)lua_tonumber(L,3);
49 color.g = (int)lua_tonumber(L,4);
50 color.b = (int)lua_tonumber(L,5);
52 temp = TTF_RenderUTF8_Blended((TTF_Font*)lua_touserdata(L,1),luaL_checkstring(L,2), color);
54 GLuint new_tex = surfaceToTexture(L, temp);
56 GLSurface *final = new GLSurface(new_tex, temp->w, temp->h);
58 SDL_FreeSurface(temp);
60 lua_pushlightuserdata(L, final);
61 lua_pushinteger(L, final->w);
62 lua_pushinteger(L, final->h);
64 return 3;