无尽的夜空,唯有月光撒满窗前

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

#

thisIsInitGL(){
loadTexture();
}
///Now the interesting code:
loadTexture()
{
        FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(textureFile,0);//Automatocally detects the format(from over 20 formats!)
        FIBITMAP* imagen = FreeImage_Load(formato, textureFile);
        
        FIBITMAP* temp = imagen;
        imagen = FreeImage_ConvertTo32Bits(imagen);
        FreeImage_Unload(temp);
        
        int w = FreeImage_GetWidth(imagen);
        int h = FreeImage_GetHeight(imagen);
        cout<<"The size of the image is: "<<textureFile<<" es "<<w<<"*"<<h<<endl; //Some debugging code
        
        GLubyte* textura = new GLubyte[4*w*h];
        char* pixeles = (char*)FreeImage_GetBits(imagen);
        //FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR).
        
        for(int j= 0; j<w*h; j++){
                textura[j*4+0]= pixeles[j*4+2];
                textura[j*4+1]= pixeles[j*4+1];
                textura[j*4+2]= pixeles[j*4+0];
                textura[j*4+3]= pixeles[j*4+3];
                //cout<<j<<": "<<textura[j*4+0]<<"**"<<textura[j*4+1]<<"**"<<textura[j*4+2]<<"**"<<textura[j*4+3]<<endl;
        }
        
        //Now generate the OpenGL texture object 
        
        glGenTextures(1, &texturaID);
        glBindTexture(GL_TEXTURE_2D, texturaID);
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textura );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        
        GLenum huboError = glGetError();
        if(huboError){
                
                cout<<"There was an error loading the texture"<<endl;
        }
        

}
posted @ 2012-01-11 13:35 skyline 阅读(938) | 评论 (1)编辑 收藏

//
// 将一些有用的Win32特性导出.
// 以便在Lua中使用.
//


extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#pragma comment(lib, "lua.lib")
};


#include <Windows.h>
#include <iostream>
using namespace std;


static const char* const ERROR_ARGUMENT_COUNT = "参数数目错误!";
static const char* const ERROR_ARGUMENT_TYPE = "参数类型错误!";


//
// 发生错误,报告错误.
//
void ErrorMsg(lua_State* luaEnv, const char* const pszErrorInfo)
{
lua_pushstring(luaEnv, pszErrorInfo);
lua_error(luaEnv);
}


//
// 检测函数调用参数个数是否正常.
//
void CheckParamCount(lua_State* luaEnv, int paramCount)
{
// lua_gettop获取栈中元素个数.
if (lua_gettop(luaEnv) != paramCount)
{
ErrorMsg(luaEnv, ERROR_ARGUMENT_COUNT);
}
}


//
// 显示Windows对话框.
// @param [in] pszMessage string 1
// @param [in] pszCaption string 2
//
extern "C" int ShowMsgBox(lua_State* luaEnv)
{
const char* pszMessage = 0;
const char* pszCaption = 0;

// 检测参数个数是否正确.
CheckParamCount(luaEnv, 2);

// 提取参数.
pszMessage = luaL_checkstring(luaEnv, 1);
pszCaption = luaL_checkstring(luaEnv, 2);

if (pszCaption && pszMessage)
{
::MessageBox(
NULL,
pszMessage,
pszCaption,
MB_OK | MB_ICONINFORMATION
);
}
else
{
ErrorMsg(luaEnv, ERROR_ARGUMENT_TYPE);
}

// 返回值个数为0个.
return 0;
}


//
// 导出函数列表.
//
static luaL_Reg luaLibs[] =
{
{"ShowMsgBox", ShowMsgBox},
{NULL, NULL}
};


//
// Dll入口函数,Lua调用此Dll的入口函数.
//
extern "C" __declspec(dllexport)
int luaopen_luadll(lua_State* luaEnv)
{
const char* const LIBRARY_NAME = "luadll";
luaL_register(luaEnv, LIBRARY_NAME, luaLibs);

return 1;
}


//:-)

posted @ 2012-01-09 20:21 skyline 阅读(498) | 评论 (0)编辑 收藏

My second tutorial dealt with calling Lua functions from C++. This one deals with just the opposite situation - calling C++ functions from Lua. I couldn't think of a simple example that I was happy with, so I borrowed the average function from the officialLua documentation.

This tutorial now covers version 5.1 of Lua. There are some pretty major differences in each version of Lua. The code shown below will not work in older versions of Lua. If you are still using an older version and don't want to upgrade, I have provided downloads for version 4.0 and 5.0 at the bottom of this tutorial. With that in mind, let's get started.

In this tutorial we will create a function in C++, tell the Lua interpreter about it, and finally call it from Lua and use the results. I will also talk a little about error checking in Lua programs.

Defining the function

The first step is to define the function. All C or C++ functions that will be called from Lua will be called using a pointer of this type:

typedef int (*lua_CFunction) (lua_State *L);

In other words, functions must have a Lua interpreter as the only argument and return only an integer. Since a Lua interpreter is used for the argument, the function can actually take any number of arguments that are read from the stack. The integer returned is actually the number of values that have been pushed on the stack as we will see later. If you have existing C++ functions that you would like to call from Lua, wrappers can easily be created to meet this requirement.

The C++ average() function given below will demonstrate accepting multiple arguments and returning more than one value. Note that it's definition matches the typedef given above.

1. The lua_gettop() function returns the index of the top of the stack. Since the stack is numbered starting from 1 in Lua, this is also the size of the stack and can be used as the number of arguments passed to the function.

2. The for loop calculates the sum all of the arguments.

3. The average of the arguments is pushed onto the stack with lua_pushnumber().

4. Then the sum of the arguments is pushed onto the stack.

5. Finally, the function returns 2, indicating two return values have been pushed to the stack.

Now that this function has been defined, we must tell the Lua interpreter about it. This is done in the main() function, just after the Lua interpreter is initialized and the libraries are loaded:

/* register our function */

lua_register(L, "average", average);

Save this file as luaavg.cpp. If you'd like to use straight C instead of C++, just name the file luaavg.c and remove the extern "C".

#include <stdio.h>

extern "C" {

#include "lua.h"

#include "lualib.h"

#include "lauxlib.h"

}

/* the Lua interpreter */

lua_State* L;

static int average(lua_State *L)

{

/* get number of arguments */

int n = lua_gettop(L);

double sum = 0;

int i;

/* loop through each argument */

for (i = 1; i <= n; i++)

{

/* total the arguments */

sum += lua_tonumber(L, i);

}

/* push the average */

lua_pushnumber(L, sum / n);

/* push the sum */

lua_pushnumber(L, sum);

/* return the number of results */

return 2;

}

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

{

/* initialize Lua */

L = lua_open();

/* load Lua base libraries */

luaL_openlibs(L);

/* register our function */

lua_register(L, "average", average);

/* run the script */

luaL_dofile(L, "avg.lua");

/* cleanup Lua */

lua_close(L);

/* pause */

printf( "Press enter to exit..." );

getchar();

return 0;

}

This simple Lua script calls the function with five arguments and prints both of the returned values. Save this as avg.lua

-- call a C++ function

avg, sum = average(10, 20, 30, 40, 50)

print("The average is ", avg)

print("The sum is ", sum)

Compiling

On Linux you can compile this program by typing this command:

g++ luaavg.cpp -llua -llualib -o luaavg

Then run the program by typing:

./luaavg

If everything worked correctly, the program should print the average and sum of the numbers.

In Visual C++ you'll need to follow these steps:

1. Create a new empty Win32 Console Application Project.

2. Add the "luaavg.cpp" file to your project.

3. On the menu click Project, luaavg Properties.

4. Expand "Configuration Properties"

5. Expand "Linker"

6. Click "Input"

7. Beside "Additional Dependencies" type lua5.1.lib.

8. Click OK

At this point, you should be able to press F7 to Build the program.

Before we can run the program, you'll need to put the "lua5.1.dll" file where Windows can find it. Copy the file from "C:\Program Files\lua5.1\lib\dll" to your new Visual C++ project's folder. If your program compiled without errors, you can now press F5 to execute the program.

Error Handling

If you've read the Lua API documentation, you know that I left the error checking out of my version of the average function above. This was done to make it easier to explain, but in a real program you will want to do some error tests. In the example above, we should at least check each argument to make sure that it is a number. This can be done by adding the following piece of code inside the for loop:

if (!lua_isnumber(L, i)) {

lua_pushstring(L, "Incorrect argument to 'average'");

lua_error(L);

}

Checks like this are easy to add and can really make debugging easier. This is very important when dealing with a program written in two different languages. You'll find yourself debugging C++ and Lua.

Downloads

· luaavg-5.1.zip - Tutorial Source and Project files for Visual C++ 2005.

· luaavg-5.1.tar.gz - Tutorial Source and Makefile for Linux.

Resources

For in depth coverage of the Lua programming language, see Programming In Lua by the creator of the language. Game Development With Lua was written by professional programmers using Lua in a game. If you're interested in using other scripting languages in your game, or even creating your own language, check out Game Scripting Mastery.

Older Versions

Here are the files for Lua 5.0.

· luaavg-5.0.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.

· luaavg-5.0.tar.gz - Tutorial Source and Makefile for Linux.

Here are the files for Lua 4.0.

· luaavg-4.zip - Tutorial Source and Project files for Visual C++ 6 on Windows.

· luaavg-4.tar.gz - Tutorial Source and Makefile for Linux.

You should now be able to set up Lua on your computer, call Lua functions from C++, and call C++ functions from Lua. This will probably be the last Lua tutorial for now. One of these days I'll come up with a more complicated program that ties all of these ideas together. Until then, feel free to experiment on your own and let me know what you come up with.

posted @ 2012-01-09 19:38 skyline 阅读(329) | 评论 (0)编辑 收藏

Calling Lua Functions---------http://gamedevgeek.com/tutorials/calling-lua-functions/

This is the second in my series of tutorials covering the Lua scripting language. The first tutorial deals with setting up Lua on Windows and Linux and compiling your first “Hello, World!” program. If you haven’t been through the first tutorial yet, please read it now.

This tutorial now covers version 5.1 of Lua. There are some pretty major differences in each version of Lua. The code shown below will not work in older versions of Lua. If you are still using an older version and don’t want to upgrade, I have provided downloads for version 4.0 and 5.0 at the bottom of this tutorial. With that in mind, let’s get started.

This tutorial will show you how to define a funcion in a Lua script and call it from your C or C++ program. We will cover passing arguments, returning values, and deal with global variables.

Your first function in Lua

Defining functions in Lua is very simple. Start with the word “function”, followed by the function name, and a list of arguments. Function definitions end with the word “end”. Functions in Lua can accept multiple arguments and return multiple results.

Here’s a simple Lua function that adds two numbers and returns their sum. Save this file as add.lua

-- add two numbers

 

function add ( x, y )

         return x + y

end

In the previous tutorial, the call to luaL_dofile() would execute the script. Since the Lua script in this tutorial only defines a function, calling luaL_dofile() simply makes the function available to our program.

As I said earlier, functions in Lua can accept multiple arguments and return multiple results. This is done using a stack.

To call a Lua funcion, we first push the function onto the stack. The function is followed by the arguments, in order. Then we call the function with lua_call(). After the function call, the return value is available on the stack. All of these steps are demonstrated in the luaadd() function below.

1.    The call to lua_getglobal() pushes the add() function onto the stack.

2.    The first argument, x, is pushed onto the stack with lua_pushnumber().

3.    The second argument, y, is pushed onto the stack with lua_pushnumer().

4.    Then the function is called with lua_call(). We specify two arguments and one return value.

5.    Now we retrieve the return value from the top of the stack with lua_tointeger().

6.    Finally, we remove the value from the stack with lua_pop().

Save this file as luaadd.cpp. If you’d like to use straight C instead of C++, just name the file luaadd.c and remove the extern “C”.

#include <stdio.h>

 

extern "C" {

         #include "lua.h"

         #include "lualib.h"

         #include "lauxlib.h"

}

 

/* the Lua interpreter */

lua_State* L;

 

int luaadd ( int x, int y )

{

         int sum;

 

         /* the function name */

         lua_getglobal(L, "add");

 

         /* the first argument */

         lua_pushnumber(L, x);

 

         /* the second argument */

         lua_pushnumber(L, y);

 

         /* call the function with 2

            arguments, return 1 result */

         lua_call(L, 2, 1);

 

         /* get the result */

         sum = (int)lua_tointeger(L, -1);

         lua_pop(L, 1);

 

         return sum;

}

 

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

{

         int sum;

 

         /* initialize Lua */

         L = lua_open();

 

         /* load Lua base libraries */

         luaL_openlibs(L);

 

         /* load the script */

         luaL_dofile(L, "add.lua");

 

         /* call the add function */

         sum = luaadd( 10, 15 );

 

         /* print the result */

         printf( "The sum is %d\\n", sum );

 

         /* cleanup Lua */

         lua_close(L);

 

         /* pause */

         printf( "Press enter to exit..." );

         getchar();

 

         return 0;

}

Compiling

On Linux you can compile this program by typing this command:

g++ luaadd.cpp -llua -ldl -o luaadd

Then run the program by typing:

./luaadd

If everything worked correctly, the program should print “The sum is 25″

In Visual C++ you’ll need to follow these steps:

1.    Create a new empty Win32 Console Application Project.

2.    Add the “luaadd.cpp” file to your project.

3.    On the menu click Project, luatest Properties.

4.    Expand “Configuration Properties”

5.    Expand “Linker”

6.    Click “Input”

7.    Beside “Additional Dependencies” type lua5.1.lib.

8.    Click OK

At this point, you should be able to press F7 to Build the program.

Before we can run the program, you’ll need to put the “lua5.1.dll” file where Windows can find it. Copy the file from “C:\Program Files\lua5.1\lib\dll” to your new Visual C++ project’s folder. If your program compiled without errors, you can now press F5 to execute the program.

Global Variables

Global variables are also easy to deal with in Lua. As we’ve seen, lua_getglobal() will push the value of a Lua global onto the stack. If our Lua script contained a global variable named z, for example, this piece of code would get it’s value:

         lua_getglobal(L, "z");

         z = (int)lua_tointeger(L, -1);

         lua_pop(L, 1);

There is also a corresponding lua_setglobal() function that sets the value of a global variable. This snippet of code demonstrates setting the value of the global Lua variable z to 10:

         lua_pushnumber(L, 10);

         lua_setglobal(L, "z");

Note that it is not necessary to explicitly define global variables in your Lua script. Setting a value with lua_setglobal() will create the global for you if it doesn’t already exist.

Downloads

·         luaadd-5.1.zip – Tutorial Source and Project files for Visual C++ 2005.

·         luaadd-5.1.tar.gz – Tutorial Source and Makefile for Linux.

Resources

For in depth coverage of the Lua programming language, see Programming In Lua by the creator of the language. Game Development With Lua was written by professional programmers using Lua in a game. If you’re interested in using other scripting languages in your game, or even creating your own language, check out Game Scripting Mastery.

Older Versions

Here are the files for Lua 5.0.

·         luaadd-5.0.zip – Tutorial Source and Project files for Visual C++ 6 on Windows.

·         luaadd-5.0.tar.gz – Tutorial Source and Makefile for Linux.

Here are the files for Lua 4.0.

·         luaadd-4.zip – Tutorial Source and Project files for Visual C++ 6 on Windows.

·         luaadd-4.tar.gz – Tutorial Source and Makefile for Linux.

You should now be able to define functions in Lua and call them from your C++ programs. The next tutorial will cover calling C++ functions from Lua.

 

posted @ 2012-01-09 19:25 skyline 阅读(377) | 评论 (0)编辑 收藏

仅列出标题
共2页: 1 2