Esempio n. 1
0
//----------------------------------------------------------------------------------------------------------------------
int main()
{
  // initialize SDL's video and audio subsystem
  if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)<0)
  {
    // or die on error
    SDLErrorExit("Unable to initialize SDL");
  }
  /// The following section is modified from :-
  /// Lazy Foo' Productions (2014). SDL Sound Effects and Music [online]. [Accessed 2014].
  /// Available from: <http://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php>.
  // initialize SDL_mixer
  if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048 )<0)
  {
      printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
      // or die on error
      SDLErrorExit("Unable to initialize SDL_mixer");
  }
  /// end of Citation

  // now get the size of the display and create a window we need to init the video
  SDL_Rect rect;
  SDL_GetDisplayBounds(0,&rect);
  // now create our window to be most of the screen's size
  SDL_Window *window=SDL_CreateWindow("SDLNGL",
                                      SDL_WINDOWPOS_CENTERED,
                                      SDL_WINDOWPOS_CENTERED,
                                      rect.w/1.2,
                                      rect.h/1.2,
                                      SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
                                     );
  // check to see if that worked or exit
  if (!window)
  {
    SDLErrorExit("Unable to create window");
  }

  // create our opengl context and attach it to our window
  SDL_GLContext glContext=createOpenGLContext(window);
  if(!glContext)
  {
    SDLErrorExit("Problem creating OpenGL context");
  }
  // make this our current GL context (we can have more than one window but in this case not)
  SDL_GL_MakeCurrent(window, glContext);
  // this makes our buffer swap syncronized with the monitor's vertical refresh
  SDL_GL_SetSwapInterval(1);
  // we need to initialise the NGL lib which will load all of the OpenGL functions, this must
  // be done once we have a valid GL context but before we call any GL commands, else
  // everything will crash
  ngl::NGLInit::instance();
  // now clear the screen and swap whilst NGL inits (which may take time)
  glClear(GL_COLOR_BUFFER_BIT);
  SDL_GL_SwapWindow(window);
  // flag to indicate if we need to exit
  bool quit=false;
  // sdl event processing data structure
  SDL_Event event;
  // bind the mouse' movement to the game, force the game to interpret the mouse' movements
  // on mac the mouse is fully locked whilst on linux the mouse is free but movements
  // are still tied to the window
  SDL_SetRelativeMouseMode(SDL_TRUE);
  // now we create an instance of a game class, this will init NGL and setup basic
  // opengl stuff ext, and when this falls out of scope the dtor will be called and cleanup
  // our gl stuff
  Game _game;
  // resize the ngl to set the screen size and camera parameters
  _game.resize(rect.w,rect.h);

  // central opengl loop
  while(!quit)
  {
    while(SDL_PollEvent(&event))
    {
      switch(event.type)
      {
        // this is the window x being clicked.
        case SDL_QUIT : quit = true; break;
        // process the mouse data by passing it to ngl class
        case SDL_MOUSEMOTION : _game.mouseMoveEvent(event.motion); break;
        // if the window is re-sized pass it to the ngl class to change gl viewport
        // note this is slow as the context is re-create by SDL each time
        // this shouldn't happen as the mouse is bound to the screen an the screen
        // shouldn't be resized
        case SDL_WINDOWEVENT :
          int w,h;
          // get the new window size
          SDL_GetWindowSize(window,&w,&h);
          _game.resize(w,h);
          break;

        // now we look for a keydown event
        case SDL_KEYDOWN:
        {
          switch( event.key.keysym.sym )
          {
            // if it's the escape key quit
            case SDLK_ESCAPE :  quit = true; break;
            // key for time-slowing
            case SDLK_s : _game.keyPressEvent(event.key.keysym); break;
            // key for attacking
            case SDLK_a : _game.keyPressEvent(event.key.keysym); break;
            // key for breaking tethers early
            case SDLK_f : _game.keyPressEvent(event.key.keysym); break;
            // key for creating a tether
            case SDLK_SPACE : _game.keyPressEvent(event.key.keysym); break;
            // key for beginning the game
            case SDLK_e : _game.keyPressEvent(event.key.keysym); break;
            default : break;
          } // end of key process
        } // end of keydown

        default : break;
      } // end of event switch
    } // end of poll events

    // all the updates
    _game.update();
    // now we draw the game
    _game.draw();
    // swap the buffers
    SDL_GL_SwapWindow(window);
  }

  // now tidy up and exit SDL and the SDL_mixer
  Mix_Quit();
  SDL_Quit();
}
Esempio n. 2
0
int main()
{

  // Initialize SDL's Video subsystem
  if (SDL_Init(SDL_INIT_VIDEO) < 0 )
  {
    // Or die on error
    SDLErrorExit("Unable to initialize SDL");
  }

  // now get the size of the display and create a window we need to init the video
  SDL_Rect rect;
  SDL_GetDisplayBounds(0,&rect);
  // now create our window
  SDL_Window *window=SDL_CreateWindow("SDLNGL",
                                      SDL_WINDOWPOS_CENTERED,
                                      SDL_WINDOWPOS_CENTERED,
                                      rect.w/2,
                                      rect.h/2,
                                      SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
                                      );
  // check to see if that worked or exit
  if (!window)
  {
    SDLErrorExit("Unable to create window");
  }

  // Create our opengl context and attach it to our window

  SDL_GLContext glContext=createOpenGLContext(window);
  if(!glContext)
  {
    SDLErrorExit("Problem creating OpenGL context");
  }
  // make this our current GL context (we can have more than one window but in this case not)
  SDL_GL_MakeCurrent(window, glContext);
  /* This makes our buffer swap syncronized with the monitor's vertical refresh */
  SDL_GL_SetSwapInterval(1);
  // we need to initialise the NGL lib which will load all of the OpenGL functions, this must
  // be done once we have a valid GL context but before we call any GL commands. If we dont do
  // this everything will crash
  ngl::NGLInit::instance();
  // now clear the screen and swap whilst NGL inits (which may take time)
  glClear(GL_COLOR_BUFFER_BIT);
  SDL_GL_SwapWindow(window);
  // flag to indicate if we need to exit
  bool quit=false;
  // sdl event processing data structure
  SDL_Event event;
  // now we create an instance of our ngl class, this will init NGL and setup basic
  // opengl stuff ext. When this falls out of scope the dtor will be called and cleanup
  // our gl stuff
  NGLDraw ngl;
  // resize the ngl to set the screen size and camera stuff
  ngl.resize(rect.w,rect.h);
  while(!quit)
  {

    while ( SDL_PollEvent(&event) )
    {
      switch (event.type)
      {
      // this is the window x being clicked.
      case SDL_QUIT : quit = true; break;
        // process the mouse data by passing it to ngl class
      case SDL_MOUSEMOTION : ngl.mouseMoveEvent(event.motion); break;
      case SDL_MOUSEBUTTONDOWN : ngl.mousePressEvent(event.button); break;
      case SDL_MOUSEBUTTONUP : ngl.mouseReleaseEvent(event.button); break;
      case SDL_MOUSEWHEEL : ngl.wheelEvent(event.wheel);
        // if the window is re-sized pass it to the ngl class to change gl viewport
        // note this is slow as the context is re-create by SDL each time
      case SDL_WINDOWEVENT :
        int w,h;
        // get the new window size
        SDL_GetWindowSize(window,&w,&h);
        ngl.resize(w,h);
        break;

        // now we look for a keydown event
      case SDL_KEYDOWN:
      {
        ngl.keyEvent(event.key);
        switch( event.key.keysym.sym )
        {
        // if it's the escape key quit
        case SDLK_ESCAPE :  quit = true; break;
        case SDLK_w : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break;
        case SDLK_s : glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break;
        case SDLK_f :
          SDL_SetWindowFullscreen(window,SDL_TRUE);
          glViewport(0,0,rect.w,rect.h);
          break;

        case SDLK_g : SDL_SetWindowFullscreen(window,SDL_FALSE); break;
        default : break;
        } // end of key process
      } // end of keydown

      default : break;
      } // end of event switch
    } // end of poll events

    // now we draw ngl
    ngl.update();
    ngl.draw();
    // swap the buffers
    SDL_GL_SwapWindow(window);

  }
  // now tidy up and exit SDL
  SDL_Quit();
}