forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
July 29, 2010, 01:41:49 PM
.
News: 06/10/2009 - New Features For Forum.
Friendly URLs, twitter for profile, and video site URLs will auto-display the video.
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read August 10, 2009, 04:29:38 PM #0
chi-kitory

Dreamcast Lua Programming

The original Tutorial can be found here.
This was rewritten for better understand and in simpler terms. Enjoy.

How to Use Lua 5.1 with the Dreamcast / KOS
KallistiOS and Lua 5.1
Before we get into this, if your new to dreamcast or just need to learn more, the best site to start is Dreamcast-Scene.com. You may want to check it out for better understand of what is involved in making Lua work with the Dreamcast.
At this point you should know how compiling for dreamcast works. First lets go to lua.org and download the programming language.

Emulator
Click here to download a dreamcast emulator (includes bios).
This will be used for testing so we dont have to make CDs For ever time we need to test our code.
May need this d3dx9_37.dll

Setting up the tutorial
-Creat a new folder for your project. ( here we will call this the root)
-Click here to download the tutoral C file "main.c" and put this in the root
-Now we are going to make our lua folder. This will contain all of your files.
-make a lua file and put this code in side of it (this will be a test "hello world")
Code:
require "Dreamcast";
Dreamcast.Write("Hello world from Lua!");
put this in the root
-Create a sub-directory within that directory called "Lua" and put all of Lua's source-files there.
-"Open up main.c. Line 152 changes the current directory (from the Dreamcast's perspective) to where the luascript file lives. You may need to change this to get the sample to work in your set-up. Google "+Dreamcast +fs_chdir" for more info if you get stuck. " (directly from the site)
-Open "lauxib.c" from the lua source, you will find this:
Code:
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  (void)ud;
  (void)osize;
  if (nsize == 0) {
      free(ptr);
    return NULL;
  }
  else
    return realloc(ptr, nsize);
}
Replace that with this:
Code:
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  (void)ud;
  (void)osize;
 
  if (nsize == 0) {
    if (osize != 0)
    {
        free(ptr);
    }
    return NULL;
  }
  else
  {
    if (osize != 0)
    {
        return realloc(ptr, nsize);
    }
    else
    {
        return malloc(nsize);

    }
  }
}
If you must understand the reasoning behind the change its on the site under "Explanation of that one change"
-To report errors correctly you may want to make another change.
open up the "luaconf.h" to find this code:
Code:
#if defined(__cplusplus)
/* C++ exceptions */
#define LUAI_THROW(L,c) throw(c)
#define LUAI_TRY(L,c,a) try { a } catch(...) \
{ if ((c)->status == 0) (c)->status = -1; }
#define luai_jmpbuf int /* dummy variable */

#elif defined(LUA_USE_ULONGJMP)
/* in Unix, try _longjmp/_setjmp (more efficient) */
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf

#else
/* default handling with long jumps */
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf

#endif
Change the "#if defined(__cplusplus)" to something like "#if defined(DONOTWANT__cplusplus)" (the point being that "DONOTWANT_cplusplus" is not defined). This seems to allow Lua to handle scripts with errors (as well as other issues most likely) without problems.

Now compile the folder for the dreamcast and test out your "hello world" :)
To add your own code, place your script in the root of the folder with the name "luascript.lua"
and your images and such in the root or sub folders. Happy Dreamcast homebrewing!

For understanding of compiling to dreamcast view these links:
http://www.dcemu.co.uk/vbulletin/archive/index.php/t-34691.html
http://www.netbsd.org/ports/dreamcast/faq.html

-Chi kitory

The emulator provided is only for lua programming testing only, we are not liable for what you do with it.
Evilmana is not liable for anything that may occur to your dreamcast when testing the files made.
Use at your own risk.
« Last Edit: August 10, 2009, 04:51:07 PM by chi-kitory »

Currently working on:
-Gundam Space adventure for PSP
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re