I am having trouble running the lua interpreter at the same time im running sdl and am also having trouble with processing events in sdl while lua is embedded basically in some pseudo code this is what it looks like so far

#include <lua.hpp>
#include <SDL/SDL.h>
#include "user.hpp"


int main(int argc,char ** argv){
      SDL_Event * Event;
      lua_State *L;
      L = lua_open();
     luaL_openlibs(L);
     int x=0;

while(x=0){
        lua_interpret(L); // a user created function
        user.sdlinit();  // another user function to initialize sdl
while(SDL_PollEvent(Event);
       user.event(Event); // user function to parse events
}
}
        lua_close();
        SDL_Quit();
return 0;
}

do note that its a little bit more messy than what I made it to look.
It compiles fine with no errors even with -Wall however when i run the program it segfaults. I double checked there doesn't seem to be any access violations all pointers are set to null upon initialization.
I just need some advice as to what may have occured even though it might not be apparent in the pseudo code above.
oh and before anyone asks no this is not for a game its just a proof of concept as i'm looking to use lua for configuration.

Edited 5 Years Ago by Celtrix: n/a

It really isn't easy to say without seeing the contents of "user.hpp"...

If I were you, I'd put cout statements in key places of the code in order to isolate the problem.

sdluac.h
Well user.hpp was a hypothetical. The above link is the actual header. I know some ones going to ask, no that repo isn't somebody elses its mine :p

ahh it won't let me edit my last post sorry.

Anyway after debuggin it with gdb I found that it stops on this line

Program received signal SIGSEGV, Segmentation fault.
0x08049207 in proc_events (Event=0x0) at stitch.h:68

line 68 in the header corresponds with this code

void proc_events(SDL_Event* myevent){
SDL_Event* place;
place = myevent;
while(SDL_PollEvent(place)){
if(place->type == SDL_QUIT){
SDL_Quit();
}
}
}

So I'm assuming I am accessing it wrong however I can't seem to find any data to the contrary. What am I doing wrong? I'm not asking for code just a point in the right direction.

I have a suspicion, I could be wrong though. Do you actually allocate memory for that event?

If you do this instead, does it work?

void proc_events()
{
    SDL_Event my_event;

    while( SDL_PollEvent( &my_event ) )
    {
        if ( my_event.type == SDL_QUIT )
            SDL_Quit();
    }
}

Edited 5 Years Ago by m4ster_r0shi: n/a

No that didn't work :( am I supposed to allocate memory for it?
I just assumed that it would automatically just as every other regular data type? The code above produces a segfault. I've tried nearly everything I can think of but I guess SDL is broken unless I am clearly doing something wrong?


Should I Just post the the entire header and main file? I can link you to them if that makes reading them easier because I host all my code on google.

Classy(basically made the code a ton more readable when I converted to Classes) . cpp
http://code.google.com/p/stitch-open-source/source/browse/june_2011/testing/sdluac/classy.cpp

sdluac.h : just the definitions
http://code.google.com/p/stitch-open-source/source/browse/june_2011/testing/sdluac/sdluac.h

Edited 5 Years Ago by Celtrix: n/a

There's a good tutorial on SDL here -> http://lazyfoo.net/SDL_tutorials/index.php

I modified some of the examples there, put them together and made this:

//The headers
#include <SDL/SDL.h>
#include <cstdio>

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *screen = NULL;
SDL_Event event;

#undef main

int main()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) return false;

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL ) return false;

    //Set the window caption
    SDL_WM_SetCaption( "event test", NULL );

    bool quit = false;

    while( !quit )
    {
        //If there is no event to handle, continue
        if ( !SDL_PollEvent( &event ) )
        {
            SDL_Delay(25);

            continue;
        }

        switch (event.type)
        {
            case SDL_QUIT:
                quit = true;

            break;
            case SDL_KEYDOWN:
                printf("a key was pressed\n");

            break;
            case SDL_KEYUP:
                printf("a key was released\n");
        }

        SDL_Delay(25);
    }

    //Quit SDL
    SDL_Quit();
}

Does this work for you?

yes it works, I tried applying a slight modification to wrap it around my program structure still I get a segfault this is really quite puzzling.

Edited 5 Years Ago by Celtrix: n/a

Well, are you really sure that the segafault is caused by the SDL
event handling procedure? What does your code look like now?

Ok i cleaned it up a bit.
Its not Segfaulting with the code below but its still not processing Events like it should.
Should I give the interpreter its own thread? it seems unlikely that it would need its own but ...
main.cpp

#include "sdluac.h"


int main(int argc,char ** argv){

SDL_Surface * screen = NULL;
SDL_Event event;
lua_State * L;
L = lua_open();
luaL_openlibs(L);
bool quit = false;
if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return false;
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);




while(!quit){
interpret_lua(L);

        if(SDL_PollEvent(&event)){
                SDL_Delay(25);
                continue;
        }
switch(event.type){
        case SDL_QUIT:
        quit = true;
        break;
}
SDL_Quit();
lua_close(L);
return 0;
}







return 0;
}

sdluac.h

#ifndef _SDLUAC_H_
#define _SDLUAC_H_
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<lua.hpp>
#include<cstdlib>
#include<SDL/SDL.h>
using namespace std;



void interpret_lua(lua_State * L){
int error;
char buf[256];
cout<<"Lua>> ";
while(fgets(buf,sizeof(buf),stdin) != NULL){
error = luaL_loadbuffer(L,buf,strlen(buf),"line") || lua_pcall(L,0,0,0);
cout<<"Lua>> ";
if(error){
fprintf(stderr,"%s\n",lua_tostring(L,-1));
lua_pop(L,1);
}
}
}
#endif

Edited 5 Years Ago by Celtrix: n/a

If what you want is to make a GUI for a lua interpreter, I suggest handling all kind of input using SDL events.
Declare a buffer variable (char array or std::string) and when you get a keydown event modify it appropriately
(e.g. if a letter is pressed add it to the buffer, if backspace is pressed pop the last char from the buffer, etc...).
If the user hits enter, run the lua interpreter on your buffer.

Edited 5 Years Ago by m4ster_r0shi: n/a

I was leaning towards using lua as a backend for the program(well to tell you the truth its a proof of concept program) all I wanted out of it was to run and close right and when ran from terminal have a lua prompt running. which I have accomplished two of the three it runs right and lua is in the back i just can't close it :(

hmm a Quick Question if I have a loop(lets say loop1) and i have loop2 so loop2 is running inside loop1 does loop1 actually continue looping or does it have to wait until loop2 is finished before starting its next iteration? the answer to this question should help me solve this weird problem.

Edited 5 Years Ago by Celtrix: n/a

How about something like this then?

SDL_event_handling_proc()
{
    get event;

    if (event == quit) quit;

    if (event == escape)
    {
        hide sdl window;

        show console

        bring console to foreground;

        call interpret_lua;

        hide console

        show sdl window;
    }
}

If you're on windows...

For showing/hiding the console window see this:
http://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx

For bringing the console window to the foreground see this:
http://www.daniweb.com/software-development/cpp/threads/370728/1594255#post1594255

EDIT:

hmm a Quick Question if I have a loop(lets say loop1) and i have loop2 so loop2 is running inside loop1 does
loop1 actually continue looping or does it have to wait until loop2 is finished before starting its next iteration?

loop1 waits until loop2 is finished.

Edited 5 Years Ago by m4ster_r0shi: n/a

so it would be impossible to have the interpreter run in tandem with the window while processsing events? I'm trying to put interpret_lua() into its own thread although I have honestly never need to use threads so as you can imagine I'm having a hard time using the pthread library.

Yes, in this case multithreading is the way to go. Well, since I have never used pthreads either (I'm a windows guy), I can't help you on this.

Dang even multi threading it didnt work it has to be something with sdl or the way my interpreter is recieving data because while i cam exit just fine and dandy it also segfaults. Anyone have any ideas about what is happening?

Edited 5 Years Ago by Celtrix: n/a

Again it wouldn't let me edit my previous post(was this by design? or just a lacking feature?).


Putting the Lua interpreter in its own thread still doesn't solve my Lua + SDL problem as I have either done it wrong(most likely that Lua is hogging CPU cycles) or SDL just hates not being the center of attention :P.

So I tried making interpret_lua() take a char array to operate on that way it doesn't lose its last value (yes I realized I could have made it static) and so that I can use interpret lua without having to have a while loop in there.

That produced the same results as all my previous code. ie SDL runs, Lua runs both are functioning just fine except that SDL refuses to parse the SDL_QUIT event. I Have tested and ran an example of SDL_QUIT so its not my package ( or rather my version of SDL ).

Any Suggestions as to why I'm not able to parse Events in SDL while running my Lua interpreter?

Edited 5 Years Ago by Celtrix: n/a

I hate to bump but this seems so trivial I don't understand why it refuses to parse the events its so simple:

initialize lua and sdl;
loop{
lua interpreter();
while(pollevent){
if ( event.type == sdl_quit){
sdl_quit();
}
}
}
return 0;

now obviously I'm using pseudo code there but why for the love of god does it refuse to parse the events? I even tried giving lua its own thread and it still doesn't parse correctly.

Edited 5 Years Ago by Celtrix: n/a

I Figured it out luaL_loadbuffer() is creating its own loop hogging my program which in turn is preventing Sdl from parsing events.

This question has already been answered. Start a new discussion instead.