GPWiki.org
GPWiki.org
It is currently Sat May 18, 2013 9:18 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Fri Jul 24, 2009 10:09 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3803
Location: Ferriday, LA, US
Hey folks, I'm learning Lua, and I can't figure out how to load data from an array of tables into my application. Basically I have some screen resolutions in a list, and I want to load them into my C++ program. So, if I have a script file like:

ScreenDefs = {}
ScreenDefs[1] = { width = 640, height = 480, fullscreen = true }
ScreenDefs[2] = { width = 800, height = 600, fullscreen = false }

For example, how do I get the data? I have figured out how to get a table and its fields, just not how to access that data if it is organized into an array.

Thanks!

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 25, 2009 10:05 pm 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
I haven't really used lua but you do know a lot of graphics libraries can query the graphics card for supported resolutions right?

for sdl:
http://www.libsdl.org/cgi/docwiki.cgi/SDL_ListModes

for sfml:
http://www.sfml-dev.org/tutorials/1.5/window-window.php

There are also platform specific code to do this if the library you are using does not support this.

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 12:01 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3803
Location: Ferriday, LA, US
Yeah, I know that already. I just want a config file for the resolutions to "search" for.

Still looking for an answer on this, appreciate the help!

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 12:31 am 
Technomaniac
User avatar

Joined: Mon Aug 23, 2004 12:24 am
Posts: 3096
Location: Washington , USA
I've got to go, but I'll try to have your answer tonight. So don't give up today :P

_________________
GMan
Black Ninja Games
Imagine ASCII art here!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 12:49 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3803
Location: Ferriday, LA, US
Waiting patiently. :))

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 1:43 am 
Octogenarian

Joined: Mon Oct 25, 2004 11:21 pm
Posts: 89
I am not a LUA expert by any stretch of the imagination...

However I believe you access it as:
screenWidth = ScreenDefs[1]["width"]

check out this website, has lots of good info
http://lua-users.org/wiki/TutorialDirectory


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 5:08 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3803
Location: Ferriday, LA, US
Well, I know how to access it in straight Lua, the problem is when I want to read the data from my C++ app.

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 6:13 am 
Level 22 Norse Warrior-Librarian
User avatar

Joined: Mon Sep 04, 2006 5:25 pm
Posts: 517
Location: U.S.
I've never worked with Lua, but if it supports pipes, that might be worth looking into :)

_________________
Worlds at War (Current Project) - http://www.awkward-games.com
Ganadu'r, The Eternal Sage (Other Current Project) - http://rpg.naget.com
Programming tutorials and web-design services: http://www.wyrmmage.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 7:18 am 
Technomaniac
User avatar

Joined: Mon Aug 23, 2004 12:24 am
Posts: 3096
Location: Washington , USA
Okay, this is quite a bit of code, well commented. (But I skipped on the error checking part, for clarity.) I also made a stack dump function, so you can visualize the stack is the code progresses. It looks like a lot (it is), but without the comments it's actually not too bad.

It's hard to see the actual output with the stack dumping in the way, so disable it to see that the code works and such, but then use it to understand how the stack manipulations work.

This code assumes your file format is the same as the example you gave in your OP. It checks the size of the table, so you can add as many definitions as you want. like I said, I didn't add error checking so if the format is wrong or whatever, you're on your own :P

Also, this program just prints the screen definitions to console, you can of course store them in a structure/class and store them in a `std::vector`, etc.

Code:
#include <lua\lua.hpp>
#include <iostream>

// uncomment this to do stack dumps
//#define DO_STACK_DUMP

// optional stack dump mischief
#ifdef DO_STACK_DUMP
   void stack_dump(lua_State *stack); // forward declare
   #define STACK_DUMP stack_dump(config); (void)0
#else
   #define STACK_DUMP (void)0
#endif

int main(void)
{
   // open state
   lua_State *config;
   config = lua_open();

   // load file
   luaL_loadfile( config, "config.lua");
   lua_pcall(config, 0, 0, 0);

   // push the table onto the stack
   lua_getglobal(config, "ScreenDefs");
   STACK_DUMP;

   // get number of elements in the table
   unsigned tableSize = lua_objlen(config, -1);

   // now get each definition
   for (unsigned i = 1; i <= tableSize; ++i)
   {
      // push which field we want to grab.
      // in lua, you can have indices of any
      // type. these are integer indices.
      lua_pushinteger(config, i);
      STACK_DUMP;

      // now get data from the table
      // lua will use the value from the top
      // to get data from the table 2 spots from the top
      lua_gettable(config, -2);
      STACK_DUMP;

      // get table replaces the index of the table by its value.
      // the top of the stack is now the value of ScreenDefs[i],
      // and the integer we pushed has been replaced by it.
      // this is another table, except unnamed. it has the fields
      // `width`, `height`, and `fullscreen`.
      // now perform the same operations to get those values.
      // push the name of the field we want to grab:
      lua_pushstring(config, "width");
      STACK_DUMP;

      // get the data from the table
      // -2 this time refers to the new table.
      // (ScreenDef's is now at position -3)
      lua_gettable(config, -2);
      STACK_DUMP;

      // the top is now the value of "width" in the table
      int width = lua_tointeger(config, -1);
      std::cout << "Width: " << width << " ";

      // remove it, so we can get the height
      lua_pop(config, 1);
      STACK_DUMP;

      // get the height
      lua_pushstring(config, "height");
      STACK_DUMP;

      // get the data associated with it
      lua_gettable(config, -2);
      STACK_DUMP;

      // the top is now the value of "height" in the table
      int height = lua_tointeger(config, -1);
      std::cout << "Height: " << height << " ";

      // remove it, so we can get fullscreen
      lua_pop(config, 1);
      STACK_DUMP;

      // get fullscreen
      lua_pushstring(config, "fullscreen");
      STACK_DUMP;

      // get the data associated with it
      lua_gettable(config, -2);
      STACK_DUMP;

      // the top is now the value of "fullscreen" in the table
      bool fullscreen = lua_toboolean(config, -1) != 0;
      std::cout << "Fullscreen: " << fullscreen << " ";

      // pop away the value
      lua_pop(config, 1);
      STACK_DUMP;

      // pop the unnamed table away, leaving only
      // the ScreenDef table, ready for the next iteration
      lua_pop(config, 1);
      STACK_DUMP;

      // (note, the last two pops could be combined into just:
      // `lua_pop(config, 2)`.
      std::cout << std::endl;
   }

   // pop the global table away,
   // finishing up the stack
   lua_pop(config, 1);
   STACK_DUMP;

   // clean up
   lua_close(config);

   return 0;
}

void stack_dump(lua_State *stack)
{
   int stackSize = lua_gettop(stack);

   std::cout << "Stack Dump: " << std::endl;

   for (int i = stackSize; i > 0; --i)
   {
      int absoluteIndex = i;
      int relativeIndex = stackSize - i + 1;

      std::cout << "Index[" << absoluteIndex << " / -" << relativeIndex << "] = ";

      int type = lua_type(stack, i);
      switch (type)
      {
      case LUA_TSTRING:
         std::cout << lua_tostring(stack, i);
         break;
      case LUA_TBOOLEAN:
         std::cout << lua_toboolean(stack, i);
         break;
      case LUA_TNUMBER:
         std::cout << lua_tonumber(stack, i);
         break;
      case LUA_TUSERDATA:
      case LUA_TLIGHTUSERDATA:
         std::cout << lua_touserdata(stack, i);
         break;
      default:
         std::cout << lua_typename(stack, type);
      }

      std::cout << std::endl;
   }

   std::cout << std::endl;
}

_________________
GMan
Black Ninja Games
Imagine ASCII art here!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 11:41 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3803
Location: Ferriday, LA, US
Pretty cool, GMan. I understand what you're doing there, so I'll tweak my routines to do what you've showed me. Thanks a load! :D

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 26, 2009 6:52 pm 
Technomaniac
User avatar

Joined: Mon Aug 23, 2004 12:24 am
Posts: 3096
Location: Washington , USA
No problem :)

_________________
GMan
Black Ninja Games
Imagine ASCII art here!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group