您尚未登录。

#1 2021-01-05 17:27:58

拉轰的脚踏车
会员
注册时间: 2020-03-20
累计积分: 168

Visual C++ 编译一个SDL2 Demo

2021-01-05_172532.png

1. 新建新项目, 通过nuget安装sdl2

2. 添加一个main.cpp文件, 文件内容 https://github.com/AlmasB/SDL2-Demo/blob/master/src/Main.cpp

#include <SDL.h>
#include <iostream>

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

int main(int arc, char ** argv) {

    if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
        std::cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
    } else {
        
        SDL_CreateWindow(
            "SDL2 Demo",
            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            SCREEN_WIDTH, SCREEN_HEIGHT,
            SDL_WINDOW_SHOWN
        );
        
        SDL_Delay(2000);
    }
    
    return 0;
}

3. main.cpp 添加内容:

#pragma comment(lib, "msvcrtd.lib")
#pragma comment(lib, "vcruntimed.lib")
#pragma comment(lib, "ucrtd.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"SDL_main\"")

4. F5 运行就出SDL2窗口了。

https://www.libsdl.org/release/SDL-1.2.15/docs/html/guidevideoopengl.html

离线

#2 2021-01-05 17:33:23

拉轰的脚踏车
会员
注册时间: 2020-03-20
累计积分: 168

Re: Visual C++ 编译一个SDL2 Demo

VC++ 2017 工程下载: Project9_sdl2_simple_demo.7z



nuget太好用了, 不需要自己处理第三方库问题。

离线

#3 2021-01-06 01:01:21

拉轰的脚踏车
会员
注册时间: 2020-03-20
累计积分: 168

Re: Visual C++ 编译一个SDL2 Demo

// To compile with gcc:  (tested on Ubuntu 14.04 64bit):
//	 g++ sdl2_opengl.cpp -lSDL2 -lGL
// To compile with msvc: (tested on Windows 7 64bit)
//   cl sdl2_opengl.cpp /I C:\sdl2path\include /link C:\path\SDL2.lib C:\path\SDL2main.lib /SUBSYSTEM:CONSOLE /NODEFAULTLIB:libcmtd.lib opengl32.lib

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <GL/gl.h>

typedef int32_t i32;
typedef uint32_t u32;
typedef int32_t b32;

#define WinWidth 1000
#define WinHeight 1000

int main (int ArgCount, char **Args)
{

  u32 WindowFlags = SDL_WINDOW_OPENGL;
  SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 0, 0, WinWidth, WinHeight, WindowFlags);
  assert(Window);
  SDL_GLContext Context = SDL_GL_CreateContext(Window);
  
  b32 Running = 1;
  b32 FullScreen = 0;
  while (Running)
  {
    SDL_Event Event;
    while (SDL_PollEvent(&Event))
    {
      if (Event.type == SDL_KEYDOWN)
      {
        switch (Event.key.keysym.sym)
        {
          case SDLK_ESCAPE:
            Running = 0;
            break;
          case 'f':
            FullScreen = !FullScreen;
            if (FullScreen)
            {
              SDL_SetWindowFullscreen(Window, WindowFlags | SDL_WINDOW_FULLSCREEN_DESKTOP);
            }
            else
            {
              SDL_SetWindowFullscreen(Window, WindowFlags);
            }
            break;
          default:
            break;
        }
      }
      else if (Event.type == SDL_QUIT)
      {
        Running = 0;
      }
    }

    glViewport(0, 0, WinWidth, WinHeight);
    glClearColor(1.f, 0.f, 1.f, 0.f);
    glClear(GL_COLOR_BUFFER_BIT);

    SDL_GL_SwapWindow(Window);
  }
  return 0;
}

https://gist.github.com/jordandee/94b187bcc51df9528a2f

SDL2 & OpenGL Simple Demo


gcc -o test2 test2.c -lSDL2 -lGL

离线

#4 2021-01-06 08:50:14

拉轰的脚踏车
会员
注册时间: 2020-03-20
累计积分: 168

Re: Visual C++ 编译一个SDL2 Demo

2021-01-06_084705.png

// To compile with gcc:  (tested on Ubuntu 14.04 64bit):
//	 g++ sdl2_opengl.cpp -lSDL2 -lGL
// To compile with msvc: (tested on Windows 7 64bit)
//   cl sdl2_opengl.cpp /I C:\sdl2path\include /link C:\path\SDL2.lib C:\path\SDL2main.lib /SUBSYSTEM:CONSOLE /NODEFAULTLIB:libcmtd.lib opengl32.lib

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <GL/gl.h>

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib, "msvcrtd.lib")
#pragma comment(lib, "vcruntimed.lib")
#pragma comment(lib, "ucrtd.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"SDL_main\"")

typedef int32_t i32;
typedef uint32_t u32;
typedef int32_t b32;

#define WinWidth 640
#define WinHeight 480

int main(int ArgCount, char **Args)
{

	u32 WindowFlags = SDL_WINDOW_OPENGL;
	SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
		WinWidth, WinHeight, WindowFlags);
	assert(Window);
	SDL_GLContext Context = SDL_GL_CreateContext(Window);

	b32 Running = 1;
	b32 FullScreen = 0;
	while (Running)
	{
		SDL_Event Event;
		while (SDL_PollEvent(&Event))
		{
			if (Event.type == SDL_KEYDOWN)
			{
				switch (Event.key.keysym.sym)
				{
				case SDLK_ESCAPE:
					Running = 0;
					break;
				case 'f':
					FullScreen = !FullScreen;
					if (FullScreen)
					{
						SDL_SetWindowFullscreen(Window, WindowFlags | SDL_WINDOW_FULLSCREEN_DESKTOP);
					}
					else
					{
						SDL_SetWindowFullscreen(Window, WindowFlags);
					}
					break;
				default:
					break;
				}
			}
			else if (Event.type == SDL_QUIT)
			{
				Running = 0;
			}
		}

		glViewport(0, 0, WinWidth/2, WinHeight/2);
		glClearColor(1.f, 0.f, 0.f, 0.5f);
		glClear(GL_COLOR_BUFFER_BIT);

		SDL_GL_SwapWindow(Window);
	}
	return 0;
}

VC++2017 工程下载: Project10_sdl2_opengl_simple_demo.7z

离线

#5 2021-01-06 17:23:49

拉轰的脚踏车
会员
注册时间: 2020-03-20
累计积分: 168

Re: Visual C++ 编译一个SDL2 Demo

https://github.com/crust/sdl2-examples/blob/master/glcontext.cpp
sdl2_opengl_test2.gif

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib, "msvcrtd.lib")
#pragma comment(lib, "vcruntimed.lib")
#pragma comment(lib, "ucrtd.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"SDL_main\"")

#include <iostream>
#include <SDL.h>
#include <SDL_opengl.h>

using std::cout;

int main(int argc, char* argv[]) {

	SDL_Init(SDL_INIT_VIDEO); // Init SDL2

	// Create a window. Window mode MUST include SDL_WINDOW_OPENGL for use with OpenGL.
	SDL_Window *window = SDL_CreateWindow(
		"SDL2/OpenGL Demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
	);

	// Create an OpenGL context associated with the window.
	SDL_GLContext glcontext = SDL_GL_CreateContext(window);

	// Now, regular OpenGL functions ...
	glMatrixMode(GL_PROJECTION | GL_MODELVIEW);
	glLoadIdentity();
	glOrtho(-320, 320, 240, -240, 0, 1);

	// ... can be used alongside SDL2.
	SDL_Event e;
	float x = 0.0, y = 30.0;
	SDL_PollEvent(&e);
	while (e.type != SDL_KEYDOWN && e.type != SDL_QUIT) {  // Enter main loop.

		SDL_PollEvent(&e);      // Check for events.

		glClearColor(0, 0, 0, 1);          // Draw with OpenGL.
		glClear(GL_COLOR_BUFFER_BIT);
		glRotatef(10.0, 0.0, 0.0, 1.0);
		// Note that the glBegin() ... glEnd() OpenGL style used below is actually 
		// obsolete, but it will do for example purposes. For more information, see
		// SDL_GL_GetProcAddress() or find an OpenGL extension loading library.
		glBegin(GL_TRIANGLES);
		glColor3f(1.0, 0.0, 0.0); glVertex2f(x, y + 90.0);
		glColor3f(0.0, 1.0, 0.0); glVertex2f(x + 90.0, y - 90.0);
		glColor3f(0.0, 0.0, 1.0); glVertex2f(x - 90.0, y - 90.0);
		glEnd();

		SDL_GL_SwapWindow(window);  // Swap the window/buffer to display the result.
		SDL_Delay(100);              // Pause briefly before moving on to the next cycle.

	}

	// Once finished with OpenGL functions, the SDL_GLContext can be deleted.
	SDL_GL_DeleteContext(glcontext);

	// Done! Close the window, clean-up and exit the program. 
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;

}

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn