• Opengl3.3+ 搭配 SDL2 框架渲染


    1.首先下载 opengl 的第三方中间件 glad 并部署,官方网址:https://glad.dav1d.de/

    2.下载 SDL2.0 并部署,官方网址:https://www.libsdl.org/download-2.0.php;

    3.当前的工程需要链接 glad(或者直接在项目中包含其源码亦可) 和 SDL2.0,glad 会动态加载底层设备厂商的实现,给我们提供较新版本的 opengl 接口;

    话不多说,代码如下(采用 VS2015 或者 VS2017 建立工程):

    #include <stddef.h>
    #include <SDL.h>
    #include "glad/glad.h"
    
    #pragma comment(lib, "SDL2.lib")
    #pragma comment(lib, "SDL2main.lib")
    
    #define SCREEN_WIDTH 800
    #define SCREEN_HEIGHT 600
    
    int main(int argc, char *argv[]) 
    {
      SDL_Window *window = NULL;
      SDL_GLContext context = NULL;
      SDL_Event event;
    
      int ret = -1;
    
      if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        return ret;
      }
    
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    
      window = SDL_CreateWindow("SDL-OpenGL3.3",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        SCREEN_WIDTH, SCREEN_HEIGHT,
        SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    
      if (!window) {
        goto end;
      }
    
      context = SDL_GL_CreateContext(window);
      gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
    
      glClearColor(1.0f, 1.0f, 0.5f, 1.0f);
    
      while (1) {
        while (SDL_PollEvent(&event)) {
          if (event.type == SDL_QUIT) {
            goto end;
          };
        }
    
        glClear(GL_COLOR_BUFFER_BIT);
    
        SDL_GL_SwapWindow(window);
        SDL_Delay(100);
      }
    
      ret = 0;
    end:
      if (context) {
        SDL_GL_DeleteContext(context);
      }
    
      if (window) {
        SDL_DestroyWindow(window);
      }
    
      SDL_Quit();
      return ret;
    }
    

      

  • 相关阅读:
    学习Timer定时器
    C++ 延时等待(sleep/timer/wait)
    MFC学习问题总结
    node.js
    总结
    关于Windows
    关于数组
    每周一次
    每周一次
    每周一次
  • 原文地址:https://www.cnblogs.com/huowenjie/p/14615067.html
Copyright © 2020-2023  润新知