sdl2 修改和添加了部分接口,并且增加了对手机系统平台(ios,andriod)的支持,显示部分改动较大,并且不向下兼容,具体可以参考官方 http://wiki.libsdl.org/MigrationGuide#Other_Renderer_API_notes
以下是使用sdl2显示一张bmp图片的demo
#include "stdafx.h"
#include "SDL.h"
#pragma comment(lib,"sdl2.lib")
int _tmain(int argc, _TCHAR* argv[])
{
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
//SDL_EventState(SDL_WINDOWEVENT_FOCUS_GAINED, SDL_IGNORE);
//SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
//SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
50, // initial x position
50, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL |SDL_WINDOW_RESIZABLE // flags - see below
);
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
//SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN);
int w =1024,h=768; //w,h应该设置为图片原始大小
SDL_Renderer* render = SDL_CreateRenderer(window, -1,0);
SDL_Texture* texture = SDL_CreateTexture(render,
<span style="white-space:pre"> </span>SDL_PIXELFORMAT_BGR24,SDL_TEXTUREACCESS_STREAMING,w,h);
FILE* f = fopen("d:\\media\\1.bmp","rb");
fseek(f,0,SEEK_END);
long size = ftell(f);
fseek(f,0,SEEK_SET);
char* buffer = new char[size];
fread(buffer,size,1,f);
int offset = *((int*)(buffer+10));
char* p = buffer + offset;
char* bmp = new char[size-offset];
for (int i=0; i< h;i++)
{
memcpy(bmp+i*1024*3,buffer+offset+(h-i-1)*1024*3,1024*3);
}
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = w;
rect.h = h;
SDL_UpdateTexture(texture,0,bmp,3*w);
SDL_RenderClear(render);
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
SDL_Event event1;
// The window is open: enter program loop (see SDL_PollEvent)
while(true){
//SDL_Delay(300); // Pause execution for 3000 milliseconds, for example
SDL_PumpEvents();
SDL_PeepEvents(&event1, 1, SDL_GETEVENT, SDL_FIRSTEVENT,SDL_LASTEVENT);
if(event1.type == SDL_WINDOWEVENT)
{
switch (event1.window.event) {
case SDL_WINDOWEVENT_RESIZED:
SDL_UpdateTexture(texture,0,bmp,3*w);
SDL_RenderClear(render);
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
break;
default :
break;
}
}
else if (event1.type==SDL_KEYDOWN)
{
switch (event1.key.keysym.sym)
{
case SDLK_f:
SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN_DESKTOP);
break;
case SDLK_g:
SDL_SetWindowFullscreen(window,0);
break;
case SDLK_q:
exit(-1);
break;
}
}
}
delete[] bmp;
delete[] buffer;
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}