SDL2.0显示图形窗口

SDL2.0基础学习。填充Surface全白色,并弹出Window显示2秒钟。

#include "stdafx.h"

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int _tmain(int argc, _TCHAR* argv[])
{
	//The window we'll be rendering to
    SDL_Window* window = NULL;
    
    //The surface contained by the window 结构体SDL_Surface是定义在结构体SDL_Window内部的
    SDL_Surface* screenSurface = NULL;

    //Initialize SDL 初始化SDL以前,不能调用任何SDL函数
	//此处,初始化视频子系统。如果有错误,SDL_Init返回-1
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
		//SDL_GetError会返回SDL函数产生的最新的错误
    }
	else
    {
        //Create window 如果有错误,SDL_CreateWindow 返回 NULL
        window = SDL_CreateWindow( "SDL Tutorial", //窗口标题 
			                       SDL_WINDOWPOS_UNDEFINED, //窗口在屏幕正中央
								   SDL_WINDOWPOS_UNDEFINED, 
								   SCREEN_WIDTH, 
								   SCREEN_HEIGHT, 
								   SDL_WINDOW_SHOWN );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }
		else
        {
            //Get window surface
            screenSurface = SDL_GetWindowSurface( window );

            //Fill the surface white
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );

			//你画了一些东西在screen surface上,不意味着你就能看到,还须要render(渲染)才行。
			//你绘画完以后,须要更新window,这样才能显示你画的内容。
			//也就是要调用下面的函数SDL_UpdateWindowSurface
            
            //Update the surface
            SDL_UpdateWindowSurface( window ); //Copy the window surface to the screen

            //Wait two seconds
            SDL_Delay( 2000 );
        }
    }

	//Destroy window 释放内存
    SDL_DestroyWindow( window );

    //Quit SDL subsystems
    SDL_Quit();

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值