SDL 2.0 如何在 windows 上使用?

https://wiki.libsdl.org/APIByCategory
http://adolfans.github.io/sdltutorialcn/sdl-2-dot-0-tutorial-index/
http://kelvmiao.info/sdl-tutorial-cn/index.html
http://www.lazyfoo.net/tutorials/SDL/index.php
// ConsoleSDL2.0.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#define SDL_MAIN_HANDLED
#include "SDL.h"
#pragma comment(lib,"SDL2.lib")
int main()
{
	
	//不使用 SDL_main(int argc, char *argv[]) 入口,需要调用下面一句函数,不然 SDL_Init 函数会调用失败,具体可官网查看 https://wiki.libsdl.org/CategoryInit
	//当然你也可以把主函数声明成和 SDL_main 一样的格式,就需要要定义 SDL_MAIN_HANDLED 宏了
	SDL_SetMainReady();
	//初始化
	if (SDL_Init(SDL_INIT_VIDEO) != 0) 
	{
		SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
		return 1;
	}

	SDL_Window *window = NULL;
	//创建窗体
	window = SDL_CreateWindow(
		"An SDL2 window",                  // window title
		SDL_WINDOWPOS_UNDEFINED,           // initial x position
		SDL_WINDOWPOS_UNDEFINED,           // initial y position
		640,                               // width, in pixels
		480,                               // height, in pixels
		SDL_WINDOW_OPENGL                  // flags - see below
	);
	if (window == NULL) {
		// In the case that the window could not be made...
		SDL_Log("Could not create window: %s\n", SDL_GetError());
		return -1;
	}

	SDL_Renderer *ren = nullptr;
	//创建渲染器
	ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (ren == nullptr) 
	{
		std::cout << SDL_GetError() << std::endl;
		return 1;
	}
	
	SDL_Surface *bmp = nullptr;
	//加载图像到内存
	bmp = SDL_LoadBMP("xxx.bmp");
	if (bmp == nullptr) 
	{
		std::cout << SDL_GetError() << std::endl;
		return 1;
	}

	SDL_Texture *tex = nullptr;
	tex = SDL_CreateTextureFromSurface(ren, bmp);

	//释放 SDL_Surface
	SDL_FreeSurface(bmp);
	//清空渲染器
	SDL_RenderClear(ren);
	//复制 SDL_Texture
	SDL_RenderCopy(ren, tex, NULL, NULL);
	//画线
	SDL_RenderDrawLine(ren, 0, 60, 120, 240);
	//更新渲染器
	SDL_RenderPresent(ren);


	while (true)
	{

	}
	//SDL_Delay(3000);

	//销毁指针
	SDL_DestroyTexture(tex);
	SDL_DestroyRenderer(ren);
	SDL_DestroyWindow(window);
	//退出
	SDL_Quit();
	//system("pause");
    return 0;
}


posted @ 2017-11-02 21:11  學海無涯  阅读(1555)  评论(0编辑  收藏  举报