#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <lua.h>
#include <lauxlib.h>
#include <SDL/SDL.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <FTGL/ftgl.h>
static void ololo(lua_State *L){
lua_newtable(L); /* We will pass a table */
/*
* To put values into the table, we first push the index, then the
* value, and then call lua_rawset() with the index of the table in the
* stack. Let's see why it's -3: In Lua, the value -1 always refers to
* the top of the stack. When you create the table with lua_newtable(),
* the table gets pushed into the top of the stack. When you push the
* index and then the cell value, the stack looks like:
*
* <- [stack bottom] -- table, index, value [top]
*
* So the -1 will refer to the cell value, thus -3 is used to refer to
* the table itself. Note that lua_rawset() pops the two last elements
* of the stack, so that after it has been called, the table is at the
* top of the stack.
*/
for (int i = 1; i <= 5; i++) {
lua_pushnumber(L, i); /* Push the table index */
lua_pushnumber(L, i*2); /* Push the cell value */
lua_rawset(L, -3); /* Stores the pair in the table */
}
/* By what name is the script going to reference our table? */
lua_setglobal(L, "foo");
}
// Тестим Lua
void testLua()
{
// Инициализация
lua_State *L = luaL_newstate();
luaL_openlibs(L);
/* Load the file containing the script we are going to run */
{
int status = luaL_loadfile(L, "scripts/script.lua");
if (status) {
/* If something went wrong, error message is at the top of */
/* the stack */
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
}
// foo = {[1]=2,[2]=4,[3]=6,[4]=8,[5]=10}
ololo(L);
/* Ask Lua to run our little script */
{
int result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
}
/* Get the returned value at the top of the stack (index -1) */
{
double sum = lua_tonumber(L, -1);
printf("Script returned: %.0f\n", sum);
}
lua_pop(L, 1); /* Take the returned value out of the stack */
lua_close(L); /* Cya, Lua */
}
void draw_triangle(float theta)
{
glPushMatrix(); //< Сохраняем состояние "модельно-видовой" матрицы
//glTranslatef(0.0f,0.0f,0.0f); //< Нет смысла перемещать на (0,0,0)
glRotatef(theta, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();
glPopMatrix();
}
void draw_text(char *text){
/* Create a pixmap font from a TrueType file. */
FTGLfont *font = ftglCreatePixmapFont("assets/verdana.ttf");
/* If something went wrong, bail out. */
if(!font){
printf("cant load font");
exit(-1);
}
/* Set the font size and render a small text. */
ftglSetFontFaceSize(font, 72, 72);
ftglRenderFont(font, text, FTGL_RENDER_ALL);
/* Destroy the font object. */
ftglDestroyFont(font); //< Почему бы шрифт не удалить? Память течь же будет
}
void testSDL()
{
SDL_Surface *screen;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(400, 400, 32, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_OPENGL);
glShadeModel(GL_SMOOTH); //< Это можно сделать один раз при начальной настройке OpenGL
// Поехали
{
SDL_Event event;
bool quit = false; //< Неплохо бы инициализировать используемые переменные
while (!quit)
{
// Обрабатываем события SDL
while (SDL_PollEvent(&event)) //< Вместо if нужно while, т.к. event'ов может прийти много
{
// printf(event.type); //< Не верно, ибо первый параметр printf() всегда строка. Надо так:
printf("%d\n",event.type);
switch (event.type)
{
case SDL_KEYDOWN:
case SDL_KEYUP:
break;
case SDL_QUIT:
quit = true;
break;
default:
break;
}
}
// ===== Рисуем
glViewport(0, 0, 400, 400);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //< Значение для очистки цветового буфера
glClearDepth(1.0); //< Значение для очистки буфера глубины
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //< Очищаем буфер
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
// Устанавливаем единичную матрицу проекции
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Устанавливаем единичную "модельно-видовую" матрицу
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
draw_triangle(0); // TESTVALUE
draw_text("ХУИТА"); // TESTVALUE
// Меняем буферы
SDL_GL_SwapBuffers();
}
}
SDL_Quit();
}
int main(int argc, char *argv[])
{
testLua();
testSDL();
return 0;
}