Example #1
0
/*
 * Create a window
 */
Window* S2D_CreateWindow(const char *title, int width, int height,
                         Update update, Render render, int flags) {
  
  // Allocate window and set default values
  Window *window = (Window*)malloc(sizeof(Window));
  window->title = title;
  window->width = width;
  window->height = height;
  window->fps_cap = 60;
  window->vsync = true;
  window->update = update;
  window->render = render;
  window->on_key = NULL;
  window->on_key_down = NULL;
  window->on_mouse = NULL;
  window->on_controller = NULL;
  window->background.r = 0.0;
  window->background.g = 0.0;
  window->background.b = 0.0;
  window->background.a = 1.0;
  
  // SDL Initialization ////////////////////////////////////////////////////////
  
  // Initialize SDL
  if (SDL_Init(SDL_INIT_EVERYTHING) != 0) S2D_Error("SDL_Init", SDL_GetError());
  
  // Initialize SDL_ttf
  if (TTF_Init() != 0) {
    S2D_Error("TTF_Init", TTF_GetError());
    free(window);
    return NULL;
  }
  
  // Initialize SDL_mixer
  int mix_flags = MIX_INIT_FLAC|MIX_INIT_OGG|MIX_INIT_MP3;
  int mix_initted = Mix_Init(mix_flags);
  if ((mix_initted&mix_flags) != mix_flags) {
    S2D_Error("Mix_Init", Mix_GetError());
  }
  
  int audio_rate = 44100;
  Uint16 audio_format = AUDIO_S16SYS;
  int audio_channels = 2;
  int audio_buffers = 4096;
  
  if (Mix_OpenAudio(audio_rate, MIX_DEFAULT_FORMAT, audio_channels, audio_buffers) != 0) {
    S2D_Error("Mix_OpenAudio", Mix_GetError());
    free(window);
    return NULL;
  }
  
  // Create SDL window
  window->sdl = SDL_CreateWindow(
    window->title,                                   // title
    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,  // window position
    window->width, window->height,                   // window size
    SDL_WINDOW_OPENGL | flags                        // flags
  );
  
  if (!window->sdl) S2D_Error("SDL_CreateWindow", SDL_GetError());
  
  // Window created by SDL might not actually be the requested size.
  // If not, retrieve and set the actual window size.
  window->s_width = window->width;
  window->s_height = window->height;
  SDL_GetWindowSize(window->sdl, &window->width, &window->height);
  
  if ((window->width != window->s_width) ||
    (window->height != window->s_height)) {
    
    sprintf(S2D_msg,
      "Resolution %dx%d unsupported by driver, scaling to %dx%d",
      window->s_width, window->s_height, window->width, window->height);
    S2D_Log(S2D_msg, S2D_WARN);
  }
  
  // Init OpenGL / GLES ////////////////////////////////////////////////////////
  
  // Specify the OpenGL Context
  #if !GLES
    if (FORCE_GL2) {
      // Use legacy OpenGL 2.1
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    } else {
      // Request an OpenGL 3.3 forward-compatible core profile
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    }
  #endif
  
  // Create and store the OpenGL context
  if (FORCE_GL2) {
    window->glcontext = NULL;
  } else {
    // Ask SDL to create an OpenGL context
    window->glcontext = SDL_GL_CreateContext(window->sdl);
  }
  
  // Check if a valid OpenGL context was created
  if (window->glcontext) {
    // Valid context found
    
    #if GLES
      // Initialize OpenGL ES 2.0
      gles_init(window->width, window->height, window->s_width, window->s_height);
      
    #else
      // Initialize OpenGL 3.3+
      gl3_init(window->width, window->height);
    #endif
    
  } else {
    // Context could not be created
    
    #if GLES
      S2D_Error("GLES / SDL_GL_CreateContext", SDL_GetError());
      
    #else
      // Try to fallback using an OpenGL 2.1 context
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
      
      // Try creating the context again
      window->glcontext = SDL_GL_CreateContext(window->sdl);
      
      // Check if this context was created
      if (window->glcontext) {
        // Valid context found
        S2D_GL2 = true;
        gl2_init(window->width, window->height);
        
      } else {
        // Could not create any OpenGL contexts, hard failure
        S2D_Error("GL2 / SDL_GL_CreateContext", SDL_GetError());
        S2D_Log("An OpenGL context could not be created", S2D_ERROR);
        free(window);
        return NULL;
      }
    #endif
  }
  
  // Store the context and print it if diagnostics is enabled
  S2D_GL_StoreContextInfo(window);
  if (diagnostics) S2D_GL_PrintContextInfo(window);
  
  return window;
}
Example #2
0
/*
 * Initialize OpenGL
 */
int S2D_GL_Init(S2D_Window *window) {

  // Specify OpenGL contexts and set attributes
  #if GLES
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  #else
    // Use legacy OpenGL 2.1
    if (FORCE_GL2) {
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    // Request an OpenGL 3.3 forward-compatible core profile
    } else {
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    }
  #endif

  // Create and store the OpenGL context
  if (FORCE_GL2) {
    window->glcontext = NULL;
  } else {
    // Ask SDL to create an OpenGL context
    window->glcontext = SDL_GL_CreateContext(window->sdl);
  }

  // Check if a valid OpenGL context was created
  if (window->glcontext) {
    // Valid context found

    // Initialize OpenGL ES 2.0
    #if GLES
      S2D_GLES_Init();
      S2D_GL_SetViewport(window);

    // Initialize OpenGL 3.3+
    #else
      // Initialize GLEW on Windows
      #if WINDOWS
        GLenum err = glewInit();
        if (GLEW_OK != err) S2D_Error("GLEW", glewGetErrorString(err));
      #endif
      S2D_GL3_Init();
      S2D_GL_SetViewport(window);
    #endif

  // Context could not be created
  } else {

    #if GLES
      S2D_Error("GLES / SDL_GL_CreateContext", SDL_GetError());

    #else
      // Try to fallback using an OpenGL 2.1 context
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

      // Try creating the context again
      window->glcontext = SDL_GL_CreateContext(window->sdl);

      // Check if this context was created
      if (window->glcontext) {
        // Valid context found
        S2D_GL2 = true;
        S2D_GL2_Init();
        S2D_GL_SetViewport(window);

      // Could not create any OpenGL contexts, hard failure
      } else {
        S2D_Error("GL2 / SDL_GL_CreateContext", SDL_GetError());
        S2D_Log(S2D_ERROR, "An OpenGL context could not be created");
        return -1;
      }
    #endif
  }

  // Store the context and print it if diagnostics is enabled
  S2D_GL_StoreContextInfo(window);
  if (S2D_diagnostics) S2D_GL_PrintContextInfo(window);

  return 0;
}