コード例 #1
0
ファイル: errorhandler.c プロジェクト: ezhangle/motor
// Expects errorhandler at index 1
void pcall(lua_State *state, int nargs) {
  if(lua_pcall(state, nargs, 0, 1)) {
    char const *msg = lua_tostring(state, -1);
    printf("Lua error: %s\n", msg);
    graphics_Font font;
    graphics_setBackgroundColor(0.64f, 0.27f, 0.26f, 1.0f);
    graphics_clear();
    graphics_setColor(1.0f, 1.0f, 1.0f, 0.8f);
    graphics_Font_new(&font, 0, 12);
    graphics_Font_render(&font, msg, 10, 40, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f); 
    graphics_swap();

    #ifdef EMSCRIPTEN
      emscripten_force_exit(1);
    #else
      exit(1);
    #endif
  }
}
コード例 #2
0
ファイル: noise.c プロジェクト: yanbin-ha/Corange
int main(int argc, char **argv) {

#ifdef _WIN32
    FILE* ctt = fopen("CON", "w" );
    FILE* fout = freopen( "CON", "w", stdout );
    FILE* ferr = freopen( "CON", "w", stderr );
#endif

    corange_init("../../assets_core");

    graphics_viewport_set_size(1280, 720);
    graphics_viewport_set_title("Noise");

    folder_load(P("./"));
    file_load(P("$CORANGE/textures/random.dds"));

    glClearColor(1.0, 0.0, 0.0, 1.0);

    ui_button* info_button = ui_elem_new("info_button", ui_button);
    ui_button_move(info_button, vec2_new(10, 10));
    ui_button_resize(info_button, vec2_new(460,25));
    ui_button_set_label(info_button, "Procedural texture from perlin noise and feedback functions");

    ui_button* save_button = ui_elem_new("save_button", ui_button);
    ui_button_move(save_button, vec2_new(480, 10));
    ui_button_resize(save_button, vec2_new(380,25));
    ui_button_set_label(save_button, "Click Here to save tileable perlin noise to file");
    ui_button_set_onclick(save_button, save_noise_to_file);

    ui_button* spinner_box = ui_elem_new("spinner_box", ui_button);
    ui_button_resize(spinner_box, vec2_new(32, 32));
    ui_button_move(spinner_box, vec2_new(870, 7));
    ui_button_set_label(spinner_box, "");

    ui_spinner* save_spinner = ui_elem_new("save_spinner", ui_spinner);
    save_spinner->color = vec4_new(1,1,1,0);
    save_spinner->top_left = vec2_new(874, 11);
    save_spinner->bottom_right = vec2_add(save_spinner->top_left, vec2_new(24,24));

    srand(time(NULL));
    shader_time = (float)rand() / (RAND_MAX / 1000);

    bool running = true;
    while(running) {
        frame_begin();

        SDL_Event event;
        while(SDL_PollEvent(&event)) {

            switch(event.type) {
            case SDL_KEYDOWN:
            case SDL_KEYUP:
                if (event.key.keysym.sym == SDLK_ESCAPE) {
                    running = 0;
                }
                if (event.key.keysym.sym == SDLK_PRINTSCREEN) {
                    graphics_viewport_screenshot();
                }
                break;
            case SDL_QUIT:
                running = 0;
                break;
                break;
            }

            ui_event(event);

        }

        shader_time += frame_time();
        ui_update();

        noise_render();
        ui_render();

        graphics_swap();

        frame_end();

    }

    SDL_WaitThread(save_thread, NULL);

    corange_finish();

    return 0;
}
コード例 #3
0
ファイル: main.c プロジェクト: AntonioModer/motor
void main_loop(void *data) {
  MainLoopData* loopData = (MainLoopData*)data;
  // TODO use registry to get love.update and love.draw?

  chdir("love");

  timer_step();
  graphics_clear();
  matrixstack_origin();
  lua_rawgeti(loopData->luaState, LUA_REGISTRYINDEX, loopData->errhand);
  lua_getglobal(loopData->luaState, "love");
  lua_pushstring(loopData->luaState, "update");

  lua_rawget(loopData->luaState, -2);
  lua_pushnumber(loopData->luaState, timer_getDelta());
  pcall(loopData->luaState, 1);

  lua_pushstring(loopData->luaState, "draw");
  lua_rawget(loopData->luaState, -2);

  pcall(loopData->luaState, 0);

  graphics_DisplayState curState = *graphics_getState();
  matrixstack_push();

  mouse_cursor_draw();

  matrixstack_pop();
  graphics_setState(&curState);

  graphics_swap();

  lua_pop(loopData->luaState, 1);

  SDL_Event event;
  while(SDL_PollEvent(&event)) {
    switch(event.type) {
    case SDL_KEYDOWN:
      keyboard_keypressed(event.key.keysym.sym);
      break;
    case SDL_KEYUP:
      keyboard_keyreleased(event.key.keysym.sym);
      break;
    case SDL_TEXTINPUT:
      keyboard_textInput(event.text.text);
      break;
    case SDL_MOUSEMOTION:
      mouse_mousemoved(event.motion.x, event.motion.y);
      break;
    case SDL_MOUSEBUTTONDOWN:
      mouse_mousepressed(event.button.x, event.button.y, event.button.button);
      break;
    case SDL_MOUSEBUTTONUP:
      mouse_mousereleased(event.button.x, event.button.y, event.button.button);
      break;
    case SDL_JOYDEVICEADDED:
      joystick_deviceAdded(event.jdevice.which);
      break;
    case SDL_JOYDEVICEREMOVED:
      joystick_deviceRemoved(event.jdevice.which);
      break;
    case SDL_JOYBUTTONUP:
      joystick_buttonReleased(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_JOYBUTTONDOWN:
      joystick_buttonPressed(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_JOYAXISMOTION:
      joystick_axisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
      break;
    case SDL_CONTROLLERBUTTONUP:
      joystick_controllerButtonReleased(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_CONTROLLERBUTTONDOWN:
      joystick_controllerButtonPressed(event.jbutton.which, event.jbutton.button);
      break;
    case SDL_CONTROLLERAXISMOTION:
      joystick_controllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
      break;


#ifndef EMSCRIPTEN
    case SDL_QUIT:
      exit(0);
#endif
    }
  }

  audio_updateStreams();
  //lua_gc(loopData->luaState, LUA_GCCOLLECT, 0);
}
コード例 #4
0
ファイル: teapot.c プロジェクト: aadarshasubedi/Corange
int main(int argc, char **argv) {
  
  #ifdef _WIN32
    FILE* ctt = fopen("CON", "w" );
    FILE* fout = freopen( "CON", "w", stdout );
    FILE* ferr = freopen( "CON", "w", stderr );
  #endif
  
  corange_init("../../assets_core");
  
  graphics_viewport_set_title("Teapot");
  graphics_viewport_set_size(1280, 720);
  
  camera* cam = entity_new("camera", camera);
  cam->position = vec3_new(5, 5, 5);
  cam->target =  vec3_new(0, 0, 0);
  
  teapot_shader = asset_hndl_new_load(P("./assets/teapot.mat"));
  teapot_object = asset_hndl_new_load(P("./assets/teapot.obj"));
  
  int running = 1;
  SDL_Event e = {0};
  
  while(running) {
    
    frame_begin();
    
    camera* cam = entity_get("camera");
    
    while(SDL_PollEvent(&e)) {
      switch(e.type){
      case SDL_KEYDOWN:
      case SDL_KEYUP:
        if (e.key.keysym.sym == SDLK_ESCAPE) { running = 0; }
        if (e.key.keysym.sym == SDLK_PRINTSCREEN) { graphics_viewport_screenshot(); }
        if (e.key.keysym.sym == SDLK_r &&
            e.key.keysym.mod == KMOD_LCTRL) {
            asset_reload_all();
        }
        break;
      case SDL_QUIT:
        running = 0;
        break;
      }
      camera_control_orbit(cam, e);
      ui_event(e);
    }
    
    ui_update();
    
    glClearColor(0.25, 0.25, 0.25, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    
    shader_program* shader = material_first_program(asset_hndl_ptr(&teapot_shader));
    shader_program_enable(shader);
    shader_program_set_mat4(shader, "world", mat4_id());
    shader_program_set_mat4(shader, "view", camera_view_matrix(cam));
    shader_program_set_mat4(shader, "proj", camera_proj_matrix(cam));
      
    shader_program_set_texture(shader, "cube_beach", 0, asset_hndl_new_load(P("$CORANGE/water/cube_sea.dds")));
    shader_program_set_vec3(shader, "camera_direction", camera_direction(cam));
    
    renderable* r = asset_hndl_ptr(&teapot_object);
    
    for(int i=0; i < r->num_surfaces; i++) {
      
      renderable_surface* s = r->surfaces[i];
      
      int mentry_id = min(i, ((material*)asset_hndl_ptr(&r->material))->num_entries-1);
      material_entry* me = material_get_entry(asset_hndl_ptr(&r->material), mentry_id);
      
      glBindBuffer(GL_ARRAY_BUFFER, s->vertex_vbo);
      
      shader_program_enable_attribute(shader, "vPosition",  3, 18, (void*)0);
      shader_program_enable_attribute(shader, "vNormal",    3, 18, (void*)(sizeof(float) * 3));
      //shader_program_enable_attribute(shader, "vTangent",   3, 18, (void*)(sizeof(float) * 6));
      //shader_program_enable_attribute(shader, "vBinormal",  3, 18, (void*)(sizeof(float) * 9));
      //shader_program_enable_attribute(shader, "vTexcoord",  2, 18, (void*)(sizeof(float) * 12));
      
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->triangle_vbo);
        glDrawElements(GL_TRIANGLES, s->num_triangles * 3, GL_UNSIGNED_INT, (void*)0);
      
      shader_program_disable_attribute(shader, "vPosition");
      shader_program_disable_attribute(shader, "vNormal");
      //shader_program_disable_attribute(shader, "vTangent");
      //shader_program_disable_attribute(shader, "vBinormal");
      //shader_program_disable_attribute(shader, "vTexcoord");
      
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

    }
  
    shader_program_disable(shader);
    
    glDisable(GL_DEPTH_TEST);
    ui_render();
    
    graphics_swap();
    
    frame_end();
  }
  
  corange_finish();
  
  return 0;
  
}
コード例 #5
0
ファイル: main.c プロジェクト: gauravalgo/CLove
void main_loop(void *data) {
  MainLoopData* loopData = (MainLoopData*)data;

  timer_step();
  lua_rawgeti(loopData->luaState, LUA_REGISTRYINDEX, loopData->errhand);
  lua_getglobal(loopData->luaState, "love");
  lua_pushstring(loopData->luaState, "update");

  lua_rawget(loopData->luaState, -2);
  lua_pushnumber(loopData->luaState, timer_getDelta());
  if (swap_At == 1){
      if(luaL_dofile(loopData->luaState, "main.lua")) {
          printf("Error: %s\n", lua_tostring(loopData->luaState, -1));
        }
    }

  if(lua_pcall(loopData->luaState, 1, 0, 0)) {
      printf("Lua error: %s\n", lua_tostring(loopData->luaState, -1));
#ifdef EMSCRIPTEN
      quit_function(loopData->luaState);
      emscripten_force_exit(1);
#else
      exit(1);
#endif
    }

  graphics_clear();

  lua_pushstring(loopData->luaState, "draw");
  lua_rawget(loopData->luaState, -2);

  if(lua_pcall(loopData->luaState, 0, 0, 0)) {
      printf("Lua error: %s\n", lua_tostring(loopData->luaState, -1));
#ifdef EMSCRIPTEN
      quit_function(loopData->luaState);
      emscripten_force_exit(1);
      l_running = 0;
#else
      l_running = 0;
#endif
    }

  graphics_swap();

  // silly hack for love.event.quit()
#ifdef WINDOWS
  event_force_quit = graphics_stop_windows();
  if(!event_force_quit)
    l_running = 0;
#endif //This will affect only Windows users
  //

  lua_pop(loopData->luaState, 1);
#ifdef UNIX
  SDL_Event event;
  while(SDL_PollEvent(&event)) {
      if (event.type == SDL_WINDOWEVENT) {
          switch (event.window.event) {
            case SDL_WINDOWEVENT_ENTER:
              graphics_setMouseFocus(1);
              break;
            case SDL_WINDOWEVENT_LEAVE:
              graphics_setMouseFocus(0);
              break;
            case SDL_WINDOWEVENT_FOCUS_LOST:
              graphics_setFocus(0);
              break;
            case SDL_WINDOWEVENT_FOCUS_GAINED:
              graphics_setFocus(1);
              break;
            default:
              break;
            }
        }
      switch(event.wheel.type)
        {
        case SDL_MOUSEWHEEL:
          mouse_mousewheel(event.wheel.y);
          int _what = event.wheel.y == 1 ? SDL_BUTTON_WHEEL_UP : SDL_BUTTON_WHEEL_DOWN;
          mouse_mousepressed(event.button.x, event.button.y,
                             _what);
          break;
        default:
          break;
        }
      switch(event.type) {
        case SDL_KEYDOWN:
          keyboard_keypressed(event.key.keysym.sym);
          break;
        case SDL_KEYUP:
          keyboard_keyreleased(event.key.keysym.sym);
          break;
        case SDL_TEXTINPUT:
          keyboard_textInput(event.text.text);
          break;
        case SDL_MOUSEMOTION:
          mouse_mousemoved(event.motion.x, event.motion.y);
          break;
        case SDL_MOUSEBUTTONDOWN:
          mouse_mousepressed(event.button.x, event.button.y,
                             event.button.button);
          break;
        case SDL_MOUSEBUTTONUP:
          mouse_mousereleased(event.button.x, event.button.y,
                              event.button.button);
          break;
#ifndef EMSCRIPTEN
        case SDL_QUIT:
          quit_function(loopData->luaState);
          l_running = 0;
#endif
        }
    }
#endif
}
コード例 #6
0
ファイル: platformer.c プロジェクト: Drooids/Corange
int main(int argc, char **argv) {
  
  #ifdef _WIN32
    FILE* ctt = fopen("CON", "w" );
    FILE* fout = freopen( "CON", "w", stdout );
    FILE* ferr = freopen( "CON", "w", stderr );
  #endif
  
  /* Init Corange, pointing to the assets_core folder */
  corange_init("../../assets_core");
  graphics_viewport_set_title("Birdy");
  graphics_viewport_set_size(800, 600);

  platformer_init();
  
  /* Set the game running, create SDL_Event struct to monitor events */
  bool running = 1;
  SDL_Event event;
  
  while(running) {
    
    /* Frame functions used to monitor frame times, FPS and other */
    frame_begin();
    
    while(SDL_PollEvent(&event)) {

      switch(event.type){
      case SDL_KEYUP:
        /* Exit on ESCAPE and Screenshot on print screen */
        if (event.key.keysym.sym == SDLK_ESCAPE) { running = false; }
        if (event.key.keysym.sym == SDLK_PRINTSCREEN) { graphics_viewport_screenshot(); }
        break;
      case SDL_QUIT:
        /* A quitting event such as pressing cross in top right corner */
        running = false;
        break;
      }
      
      /* Also send this event off to the game and ui */
      platformer_event(event);
      ui_event(event);
    }
    
    platformer_update();
    ui_update();
    
    platformer_render();
    ui_render();
    
    /* Flip the Screen Buffer. We've finished with this frame. */
    graphics_swap(); 
    
    /* This allows us to fix the framerate to 60 fps, even on my laptop with vsync broken */
    frame_end();
  }
  
  platformer_finish();
  
  /* Corange will unload remaining assets and delete any remaining entities */
  corange_finish();
  
  return 0;
}