main() { char **tokenv; char *s; int tokenc; int i; prompt(); for ( s=read_long(); s!=NULL; s=read_long() ) { tokenv=tokenize(s,&tokenc); if (!no_token(tokenv)) /* if there is a token */ { check_for_exit(tokenv); do_fork(tokenv,&tokenc); } prompt(); } }
int main() { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); video v; input in; chip8cpu c8cpu; init_video(&v); chip8_init(&c8cpu); chip8_load_resources(&c8cpu, &v, &in); load_program(&c8cpu); for (;;) { /* parse_instruction(&c8cpu, 0x620A); // set r2 to 0A parse_instruction(&c8cpu, 0x6308); // set r3 to 09 parse_instruction(&c8cpu, 0x8235); // subtract r3 from r2 parse_instruction(&c8cpu, 0xA003); // set I to 123 parse_instruction(&c8cpu, 0xF555); // move r4 to [I] */ if (check_for_exit(c8cpu.p_input)) break; SDL_Flip(c8cpu.p_video->screen); SDL_Delay(250); break; } print_chip8_state(&c8cpu); main_memory_dump(&c8cpu); SDL_Quit(); return 0; }
/* main - function - entry point */ int main(int argc, char *argv[]) { lua_State *L; SDL_Event event; Uint32 lastTick; /* Last iteration's tick value */ Uint32 delta = 0; int i, n, narg; char *filename = NULL; fprintf(stdout, "%s v%s\n", PROG_NAME, VERSION); if (argc > 1) { n = 1; if (strcmp(argv[1], "--") != 0) filename = argv[1]; } else { n = argc - 1; } L = luaL_newstate(); /* initialize Lua */ luaL_openlibs(L); /* load Lua base libraries */ FS_Init(L, argv, &filename); /* initialize virtual filesystem */ /* register Lua functions */ lua_newtable(L); luaopen_main(L, NULL); luaopen_fileio(L, NULL); luaopen_font(L, NULL); luaopen_graphics(L, NULL); luaopen_sound(L, NULL); luaopen_mouse(L, NULL); luaopen_keyboard(L, NULL); luaopen_movie(L, NULL); lua_setglobal(L, NAMESPACE); /* put luaopen_oocairo in package.preload["oocairo"] */ lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_pushcfunction(L, luaopen_oocairo); lua_setfield(L, -2, "oocairo"); /* put luaopen_socket_core in package.preload["socket.core"] */ lua_pushcfunction(L, luaopen_socket_core); lua_setfield(L, -2, "socket.core"); /* put luaopen_mime_core in package.preload["mime.core"] */ lua_pushcfunction(L, luaopen_mime_core); lua_setfield(L, -2, "mime.core"); lua_pop(L, 2); /* push the error function for the protected calls later on */ lua_pushcfunction(L, error_function); /* load and compile main lua file */ if (FS_loadFile(L, filename) == FILEIO_ERROR) { error(L, "Error loading '%s':\n\t%s", filename, lua_tostring(L, -1)); } /* push all script arguments */ /* create arg-table for the command-line-arguments */ /* copied from lua.c of the Lua distribution */ narg = argc - (n+1); /* number of arguments to the script */ luaL_checkstack(L, narg+2, "too many arguments to script"); for (i=n+1; i<argc; i++) { lua_pushstring(L, argv[i]); } lua_createtable(L, narg, n+1); for (i=0; i<argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i - n); } lua_setglobal(L, "arg"); /* run the compiled chunk */ if ((lua_pcall(L, narg, 0, -(narg+2)) != 0) && !check_for_exit(L)) { error(L, "Error running '%s':\n\t%s", filename, lua_tostring(L, -1)); } /* the error function stays on the stack */ if (SDL_GetVideoSurface() == NULL) error(L, "Error: " PROG_NAME " was not initialized by " NAMESPACE ".init()!\n"); /* main loop */ /* Wait until SDL_QUIT event type (window closed) or a call to scrupp.exit() occurs */ while ( !done ) { lastTick = SDL_GetTicks(); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); /* maybe the 'main' table has changed so get a new reference in every cycle */ lua_getglobal(L, "main"); if (lua_isnil(L, -1)) { error(L, "Error: Table 'main' not found!\n"); } /* at this point, the stack always contains the error function and the 'main' table */ /* main.render(delta) */ lua_getfield(L, -1, "render"); lua_pushinteger(L, delta); if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) { error(L, "Error running main.render:\n\t%s\n", lua_tostring(L, -1)); } SDL_GL_SwapBuffers(); while ( SDL_PollEvent( &event ) ) { switch ( event.type ) { case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: lua_getfield(L, -1, "keypressed"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushliteral(L, "Scrupp:key_table"); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, event.key.keysym.sym); lua_remove(L, -2); /* remove the key_table */ if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) { error(L, "Error running main.keypressed:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_KEYUP: lua_getfield(L, -1, "keyreleased"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushliteral(L, "Scrupp:key_table"); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, event.key.keysym.sym); lua_remove(L, -2); /* remove the key_table */ if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) { error(L, "Error running main.keyreleased:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_MOUSEBUTTONDOWN: lua_getfield(L, -1, "mousepressed"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushinteger(L, event.button.x); lua_pushinteger(L, event.button.y); lua_pushstring(L, buttonNames[event.button.button-1]); if ((lua_pcall(L, 3, 0, -6) != 0) && !check_for_exit(L)) { error(L, "Error running main.mousepressed:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_MOUSEBUTTONUP: lua_getfield(L, -1, "mousereleased"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushinteger(L, event.button.x); lua_pushinteger(L, event.button.y); lua_pushstring(L, buttonNames[event.button.button-1]); if ((lua_pcall(L, 3, 0, -6) != 0) && !check_for_exit(L)) { error(L, "Error running main.mousereleased:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_VIDEORESIZE: lua_getfield(L, -1, "resized"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushinteger(L, event.resize.w); lua_pushinteger(L, event.resize.h); if ((lua_pcall(L, 2, 0, -5) != 0) && !check_for_exit(L)) { error(L, "Error running main.resized:\n\t%s\n", lua_tostring(L, -1)); } break; } } lua_pop(L, 1); /* pop 'main' table */ delta = SDL_GetTicks() - lastTick; /* if (delta>1) { fprintf(stdout, "delta: %d\n", delta); } */ while (delta < minimumDelta) { SDL_Delay(1); delta = SDL_GetTicks() - lastTick; } } lua_close(L); return 0; }