void sdl_application::execute(window& win)
{
	signal_init();

	SDL_GL_SetSwapInterval(1);

#ifdef EMSCRIPTEN

	emscripten_set_main_loop_arg(update_c_function, this, -1, 1);

#else

	SDL_GL_SetSwapInterval(0);

	while (running)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		update_c_function(this);
		SDL_GL_SwapWindow(win.m_window);
	}

#endif

	signal_quit();
}
Exemplo n.º 2
0
int main( int argc, char* args[] )
{
    Particles emitter;

    //Start up SDL and create window
    if ( init(&gWindow, &renderer) )
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {

        if (load_texture(&smokeTexture,  renderer) == 1) {
            printf("Failed to load texture\n");
        }
        else
        {


            initParticles( &emitter, 170, 315 );

            printf("in main %f\n", emitter.m_speed);
            emitter.m_lastRender = getMilliCount();

            //Main loop flag

#ifdef EMSCRIPTEN
            emscripten_set_main_loop_arg((em_arg_callback_func)loop, (void*)&emitter, 0, 1);
#else
            while (!quit) {
                SDL_Delay(15);
                loop(&emitter);
            }
#endif

        }
    }

    //Free resources and close SDL
    SDL_DestroyTexture(smokeTexture);
    smokeTexture = NULL;

    SDL_DestroyRenderer(renderer);
    renderer = NULL;

    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    IMG_Quit();
    SDL_Quit();

    return 0;
}
Exemplo n.º 3
0
static SDL_bool
WatchJoystick(SDL_Joystick * joystick)
{
    SDL_Window *window = NULL;
    const char *name = NULL;

    retval = SDL_FALSE;
    done = SDL_FALSE;

    /* Create a window to display joystick axis position */
    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
                              SCREEN_HEIGHT, 0);
    if (window == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
        return SDL_FALSE;
    }

    screen = SDL_CreateRenderer(window, -1, 0);
    if (screen == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        return SDL_FALSE;
    }

    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(screen);
    SDL_RenderPresent(screen);
    SDL_RaiseWindow(window);

    /* Print info about the joystick we are watching */
    name = SDL_JoystickName(joystick);
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
           name ? name : "Unknown Joystick");
    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));

    /* Loop, getting joystick events! */
#ifdef __EMSCRIPTEN__
    emscripten_set_main_loop_arg(loop, joystick, 0, 1);
#else
    while (!done) {
        loop(joystick);
    }
#endif

    SDL_DestroyRenderer(screen);
    screen = NULL;
    SDL_DestroyWindow(window);
    return retval;
}
Exemplo n.º 4
0
extern "C" int main( int argc, char* args[] ){
	srand(time(NULL));
	std::cout << "[C++] Begin program\n" << std::endl;

	gdisplay.init();
	ggame.init();
	gdisplay.onresize = &onresize;

	MainLoopScope scope = MainLoopScope( &gdisplay, &ggame );
	emscripten_set_main_loop_arg(_main_loop, (void*)(&scope), 0, 1);

	return 0;
}
Exemplo n.º 5
0
static void sdl_main_loop(void *ctx)
{
    platform_ctx *platform = (platform_ctx *)ctx;
#ifdef EMSCRIPTEN
    emscripten_set_main_loop_arg(platform->onFrame, platform->user, 0, 1);
#else
    platform->startTime = SDL_GetTicks();
    while (!platform->done)
    {
        platform->onFrame(platform->user);
        if (platform->keys[KEY_ESC])
            platform->done = 1;
    }
#endif
}
Exemplo n.º 6
0
int main( int argc, const char* argv[] ) {	
	try
	{
		sampleInst = new Context();
		sampleInst->initApp();
	    emscripten_set_main_loop_arg(Context::_mainLoop, sampleInst, 0, 1);
		sampleInst->closeApp();
        delete sampleInst;
        sampleInst = NULL;
	}
	catch (std::exception& e)
	{
		emscripten_run_script((std::string("alert('") + e.what() + "')").c_str());
	}
	return 0;
}
Exemplo n.º 7
0
void mainey() {
  static int counter = 0;
  printf("mainey: %d\n", counter++);
  if (counter == 20) {
    emscripten_pause_main_loop();
    emscripten_async_call(five, (void*)55, 1000);
  } else if (counter == 22) { // very soon after 20, so without pausing we fail
    assert(fived);
    emscripten_push_main_loop_blocker(pre1, (void*)123);
    emscripten_push_main_loop_blocker(pre2, (void*)98);
  } else if (counter == 23) {
    assert(pre1ed);
    assert(pre2ed);
    printf("Good!\n");
    emscripten_cancel_main_loop();
    emscripten_set_main_loop_arg(argey, (void*)17, 0, 0);
  }
}
int LevelScene::exec()
{
    m_isLevelContinues = true;
    m_doExit = false;
    m_isRunning = true;
    times.init();
    times.start_common = SDL_GetTicks();
    //Set black color clearer
    GlRenderer::setClearColor(0.f, 0.f, 0.f, 1.0f);

    /****************Initial update***********************/
    //Apply musics and backgrounds
    for(size_t i = 0; i < m_cameras.size(); i++)
    {
        //Play music from first camera only
        if(i == 0)
            m_cameras[i].cur_section->playMusic();
        m_cameras[i].cur_section->initBG();
    }

    //(Need to prevent accidental spawn of message box or pause menu with empty screen)
    m_player1Controller->resetControls();
    m_player2Controller->resetControls();

    if(m_isRunning)
        update();

    debug_TimeCounted = 0;
    debug_TimeReal.restart();
    /*****************************************************/

    #ifndef __EMSCRIPTEN__
    while(m_isRunning)
        levelSceneLoopStep(this);
    #else
    emscripten_set_main_loop_arg(levelSceneLoopStep, this, (int)PGE_Window::frameRate, 1);
    #endif

    return m_exitLevelCode;
}
Exemplo n.º 9
0
static int emscripten_setup(struct sim_state *s, const struct run_ops *ops,
        void **run_data, void *ops_data)
{
    struct emscripten_wrap data = {
        .s = s,
        .ops = ops,
        .run_data = run_data,
        .ops_data = ops_data,
        .insns_per_frame = 1,
    };

    param_get_int(s->conf.params, "emscripten.insns_per_anim_frame", &data.insns_per_frame);
    emscripten_cancel_main_loop(); // In case we get called more than once
    emscripten_set_main_loop_arg(emscripten_wrap_step, &data, 0, 1);

    return data.rc;
}

int recipe_emscript(struct sim_state *s)
{
    s->run_sim = emscripten_setup;
    return 0;
}
Exemplo n.º 10
0
int main() {
  lua_State *lua = luaL_newstate();
  luaL_openlibs(lua);


  motor_Config config;
  l_motor_register(lua);
  l_audio_register(lua);
  l_graphics_register(lua);
  l_image_register(lua);
  l_keyboard_register(lua);
  l_mouse_register(lua);
  l_filesystem_register(lua);
  l_timer_register(lua);
  l_math_register(lua);
  l_event_register(lua);
  l_joystick_register(lua);

  chdir("/love");
  l_boot(lua, &config);

  if(config.identity) {
    filesystem_setIdentity(config.identity, false);
  }

  image_init();
  joystick_init();
  keyboard_init();
  graphics_init(config.window.width, config.window.height);
  audio_init();
  math_init();


  if(luaL_dofile(lua, "main.lua")) {
    printf("Error: %s\n", lua_tostring(lua, -1));
  }

  lua_pushcfunction(lua, errorhandler);
  lua_getglobal(lua, "love");
  lua_pushstring(lua, "load");
  lua_rawget(lua, -2);
  /*if(lua_pcall(lua, 0, 0, 1)) {
    printf("Error in love.load: %s\n", lua_tostring(lua, -1));
  }
  */
  pcall(lua, 0);
  lua_pop(lua, 1);

  lua_pushcfunction(lua, errorhandler);

  mainLoopData.luaState = lua;
  mainLoopData.errhand = luaL_ref(lua, LUA_REGISTRYINDEX);

  timer_init();
#ifdef EMSCRIPTEN
  emscripten_set_main_loop_arg(main_loop, &mainLoopData, 0, 1);
#else
  for(;;) {
    main_loop(&mainLoopData);
  }
#endif
}
SDL_bool WatchGameController(SDL_GameController *gamecontroller) {
  const char *name = SDL_GameControllerName(gamecontroller);
  const char *basetitle = "Game Controller Test: ";
  const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
  char *title = (char *)SDL_malloc(titlelen);
  SDL_Window *window = NULL;

  retval = SDL_FALSE;
  done = SDL_FALSE;

  if (title) {
    SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
  }

  /* Create a window to display controller state */
  window =
      SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                       SCREEN_WIDTH, SCREEN_HEIGHT, 0);
  if (window == NULL) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n",
                 SDL_GetError());
    return SDL_FALSE;
  }

  screen = SDL_CreateRenderer(window, -1, 0);
  if (screen == NULL) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n",
                 SDL_GetError());
    SDL_DestroyWindow(window);
    return SDL_FALSE;
  }

  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  SDL_RenderClear(screen);
  SDL_RenderPresent(screen);
  SDL_RaiseWindow(window);

  /* scale for platforms that don't give you the window size you asked for. */
  SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);

  background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
  button = LoadTexture(screen, "button.bmp", SDL_TRUE);
  axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);

  if (!background || !button || !axis) {
    SDL_DestroyRenderer(screen);
    SDL_DestroyWindow(window);
    return SDL_FALSE;
  }
  SDL_SetTextureColorMod(button, 10, 255, 21);
  SDL_SetTextureColorMod(axis, 10, 255, 21);

  /* !!! FIXME: */
  /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/

  /* Print info about the controller we are watching */
  SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");

/* Loop, getting controller events! */
#ifdef __EMSCRIPTEN__
  emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
#else
  while (!done) {
    loop(gamecontroller);
  }
#endif

  SDL_DestroyRenderer(screen);
  screen = NULL;
  background = NULL;
  button = NULL;
  axis = NULL;
  SDL_DestroyWindow(window);
  return retval;
}
Exemplo n.º 12
0
int main(int argc, char* argv[]) {
  lua_State *lua = luaL_newstate();
  luaL_openlibs(lua);

  love_Config config;

  l_love_register(lua);
  l_audio_register(lua);
  l_event_register(lua);
  l_graphics_register(lua);
  l_image_register(lua);
  l_keyboard_register(lua);
  l_mouse_register(lua);
  l_filesystem_register(lua);
  l_timer_register(lua);
  l_math_register(lua);
  l_system_register(lua);
  l_physics_register(lua);

  l_boot(lua, &config);

  keyboard_init();
  graphics_init(config.window.width, config.window.height);
  audio_init();

  if(luaL_dofile(lua, "main.lua")){
      printf("Error: %s\n", lua_tostring(lua, -1));
      l_no_game(lua,&config);
    }

  love_Version const * version = love_getVersion();
  printf("%s %s %i %i %i \n", "Love code name: ",version->codename,version->major,version->minor,version->revision);

  lua_pushcfunction(lua, lua_errorhandler);
  lua_getglobal(lua, "love");
  lua_pushstring(lua, "load");
  lua_rawget(lua, -2);
  if(lua_pcall(lua, 0, 0, 1)) {
      printf("Error in love.load: %s\n", lua_tostring(lua, -1));
    }
  lua_pop(lua, 1);

  lua_pushcfunction(lua, lua_errorhandler);
  MainLoopData mainLoopData = {
    .luaState = lua,
    .errhand = luaL_ref(lua, LUA_REGISTRYINDEX)
  };

  timer_init();

#ifdef EMSCRIPTEN
  //TODO find a way to quit(love.event.quit) love on web?
  emscripten_set_main_loop_arg(main_loop, &mainLoopData, 0, 1);
#else
  while(l_event_running()) {
      main_loop(&mainLoopData);
    }
  if(!l_event_running())
    quit_function(lua);
#endif
  audio_close ();
  lua_close(lua);
  return 0;
}
Exemplo n.º 13
0
GHL_API int GHL_CALL GHL_StartApplication( GHL::Application* app , int /*argc*/, char** /*argv*/) {
    
    g_application = app;
    
    LOG_INFO(  "start" );
    SDL_Init(SDL_INIT_VIDEO);


    GHL::VFS* vfs = GHL_CreateVFS();


    g_application->SetVFS(vfs);

    SystemSDL system;
    g_application->SetSystem(&system);

    {
        GHL::Event e;
        e.type = GHL::EVENT_TYPE_APP_STARTED;
        g_application->OnEvent(&e);
    }

    GHL::Settings settings;
    settings.width = 800;
    settings.height = 600;
    settings.depth = false;
    app->FillSettings(&settings);

    GHL::ImageDecoderImpl image_decoder;
    g_application->SetImageDecoder(&image_decoder);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    if (settings.depth) {
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
    }
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    
    g_window = SDL_CreateWindow(
        "GHL", 0, 0, settings.width, settings.height, 
        SDL_WINDOW_OPENGL);

    SDL_GLContext glcontext = SDL_GL_CreateContext(g_window);

    g_height = settings.height;

    g_render = GHL_CreateRenderOpenGL(settings.width,settings.height,settings.depth);
    if ( g_render && g_application ) {
        g_application->SetRender(g_render);
        g_application->Load();
    } else {
         LOG_ERROR(  "not started" );
    }

    g_last_time = SDL_GetTicks();

    g_done = false;

#ifdef GHL_PLATFORM_EMSCRIPTEN
    emscripten_set_main_loop_arg((em_arg_callback_func)loop_iteration, g_window, 0, 1);
#else
    while (!g_done) {
        loop_iteration(g_window);
    }
#endif

    LOG_INFO(  "done" );
    SDL_GL_DeleteContext(glcontext);  
    // Cleanup
   
    GHL_DestroyVFS(vfs);
    SDL_Quit();

    return 0;
}
Exemplo n.º 14
0
int emulate(smn8_rom *rom)
{
	SDL_Window *window;
	SDL_Surface *screen;
#ifdef HAVE_AUDIO
	SDL_AudioSpec audio_spec;
#endif
	smn8_vm vm;
	emulator_state state;

#ifdef HAVE_AUDIO
	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
#else
	if(SDL_Init(SDL_INIT_VIDEO) != 0)
#endif
	{
		fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
		return EXIT_FAILURE;
	}

#ifdef HAVE_AUDIO
	SDL_zero(audio_spec);

	audio_spec.freq = 22050;
	audio_spec.format = AUDIO_U16;
	audio_spec.channels = 2;
	audio_spec.samples = 4096;
	audio_spec.callback = audio;

	if(SDL_OpenAudio(&audio_spec, NULL) != 0)
	{
		fprintf(stderr, "Failed to initialize SDL audio: %s\n", SDL_GetError());
		SDL_Quit();
		return EXIT_FAILURE;
	}
#endif

	window = SDL_CreateWindow("SMN8", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
									  WIDTH, HEIGHT, 0);

	if(window == NULL)
	{
		fprintf(stderr, "Failed to create SDL Window: %s\n", SDL_GetError());
#ifdef HAVE_AUDIO
		SDL_CloseAudio();
#endif
		SDL_Quit();
		return EXIT_FAILURE;
	}

	SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "1");

	screen = SDL_GetWindowSurface(window);
	if(screen == NULL)
	{
		fprintf(stderr, "Failed to get a valid SDL Surface: %s\n", SDL_GetError());
		SDL_DestroyWindow(window);
#ifdef HAVE_AUDIO
		SDL_CloseAudio();
#endif
		SDL_Quit();
		return EXIT_FAILURE;
	}

	state.colors[COLOR_BG] = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);

#ifdef HAVE_LIME
	state.colors[COLOR_FG] = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
#else
	state.colors[COLOR_FG] = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
#endif

	SDL_FillRect(screen, NULL, state.colors[COLOR_BG]);

	state.vm = &vm;
	state.screen = screen;
	state.window = window;
#ifndef __EMSCRIPTEN__
	state.ticks = SDL_GetTicks();
#endif

	if(!smn8_vm_init(&vm, rom))
	{
		smn8_vm_clear(&vm);
		SDL_DestroyWindow(window);
#ifdef HAVE_AUDIO
		SDL_CloseAudio();
#endif
		SDL_Quit();
	}

#ifdef __EMSCRIPTEN__
	emscripten_set_main_loop_arg(tick, &state, 0, 1);
#else
	for(;;)
	{
		if(!tick(&state))
			break;
	}

	smn8_vm_clear(&vm);

	SDL_DestroyWindow(window);
#ifdef HAVE_AUDIO
	SDL_CloseAudio();
#endif

	SDL_Quit();
#endif
	return EXIT_SUCCESS;
}