sdl2-2.04 读取位图并显示

本文介绍了一个使用SDL2库创建简单窗口并显示图片的基本示例。通过初始化SDL库、设置视频模式、加载位图文件、将位图绘制到窗口表面等步骤,实现了基本的图形显示功能,并通过监听键盘事件来控制程序退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// sdl2_win32.cpp : Defines the entry point for the console application.
//
// 假定SDL的库文件和头文件和VC的工程文件在一起.
#include "stdafx.h"
#include "SDL2-2.0.4/include/SDL.h"
#pragma comment(lib, "SDL2-2.0.4/lib/x86/sdl2.lib")

#include <iostream>

void pressESCtoQuit();

int _tmain(int argc, _TCHAR* argv[])
{
    try {
        if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
            throw SDL_GetError();
    }
    catch ( const char* s ) {
        std::cerr << "SDL_Init() failed!\n" << s << std::endl;
        return -1;
    }

    const int SCREEN_WIDTH = 640;    // 0 means use current width.
    const int SCREEN_HEIGHT = 480;    // 0 means use current height.
    const int SCREEN_BPP = 32;        // 0 means use current bpp.
    const Uint32 SCREEN_FLAGS = SDL_SWSURFACE;    // SDL_SWSURFACE == 0,surface in system memory.

    SDL_Surface* pScreen = 0;
    SDL_Window* win = SDL_CreateWindow("title", 0,0,SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_FLAGS); // SDL1.X叫SDL_SetVideoMode()
    pScreen = SDL_GetWindowSurface(win); // 相比SDL1.X多一步
    try {
        if ( pScreen == 0 )
            throw SDL_GetError();
    }
    catch ( const char* s ) {
        std::cerr << "SDL_SetVideoMode() failed!\n" << s << std::endl;
        SDL_Quit();
        return -1;
    }

    SDL_Surface* pShownBMP = 0;
    pShownBMP = SDL_LoadBMP("hello.bmp"); // Load a BMP file, and convert it as a surface.
    try {
        if ( pShownBMP == 0 )
            throw SDL_GetError();
    }
    catch ( const char* s ) {
        std::cerr << "SDL_LoadBMP() failed!\n" << s << std::endl;
        SDL_Quit();
        return -1;
    }

    SDL_Rect* pSrcRect = 0;    // If pSrcRect is NULL, the entire source surface is copied.

    SDL_Rect* pDstRect = 0;    // If pDstRect is NULL, then the destination position (upper left corner) is (0, 0).
    try {
        if ( SDL_BlitSurface(pShownBMP, pSrcRect, pScreen, pDstRect) != 0 )    // Put the BMP's surface on the SDL window's surface.
            throw SDL_GetError();
    }
    catch ( const char* s ) {
        std::cerr << "SDL_BlitSurface() failed!\n" << s << std::endl;
        SDL_Quit();
        return -1;
    }
    
    try {
        if ( SDL_UpdateWindowSurface(win) != 0 )    // Show the SDL window's surface. SDL1.X叫SDL_Flip
          throw SDL_GetError();
    }
    catch ( const char* s ) {
        std::cerr << "SDL_Flip() failed!\n" << s << std::endl;
        SDL_Quit();
        return -1;
    }

    pressESCtoQuit();
    SDL_Quit();

    return 0;
}

void pressESCtoQuit()
{
    bool gameOver = false;
    while( gameOver == false ){
        SDL_Event gameEvent;
        while ( SDL_PollEvent(&gameEvent) != 0 ){
            if ( gameEvent.type == SDL_QUIT ){
                gameOver = true;
            }
            if ( gameEvent.type == SDL_KEYUP ){
                if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
                    gameOver = true;
                }
            }
        }
    }
    return;
}

转载于:https://www.cnblogs.com/agnt/p/5134245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值