// 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;
}

sdl2-2.04 读取位图并显示的更多相关文章

  1. opencv从txt文本读取像素点并显示

    opencv从txt文本读取像素点并显示 文本储存格式为每行一个像素点,排列为RGB.每帧图像的帧头为65535.  如下图所示 废话不多说,代码如下: // #include <iostrea ...

  2. 从多个XML文档中读取数据用于显示webapi帮助文档

    前言: 你先得知道HelpPageConfig文件,不知道说明你现在不需要这个,所以下文就不用看了,等知道了再看也不急.当然如果你很知道这个,下文也不用看了,因为你会了. 方法一: new XmlDo ...

  3. MFC中位图的显示

    分析: 首先,我们要明确一点,窗口的绘制包括两个步骤,首先:擦除窗口背景,然后再对窗口重新进行绘制:当擦除窗口背景时,程序会发生一个WM_ERASEBKGND消息,因此可以在此响应函数中完成位图的显示 ...

  4. [问题解决]Fresco设置占位图不显示的问题

    [问题解决]Fresco设置占位图不显示的问题 /** * Created by diql on 2017/02/15. */ 问题说明 本来设置占位图是通过以下方法: public void set ...

  5. 【WPF学习笔记】之如何把数据库里的值读取出来然后显示在页面上:动画系列之(六)(评论处有学习资料及源码)

    (应博友们的需要,在文章评论处有源码链接地址,以及WPF学习资料.工具等,希望对大家有所帮助) ...... 承接系列五 上一节讲了,已经把数据保存到数据库并且删除数据,本讲是把已经存在的数据从数据库 ...

  6. Ext.net自动保存读取GrdPanel列显示状态

    //layout保存 function SaveLayOut() { let colVisibleArray = []; for (var i = 0; i < mcp_gridlist.col ...

  7. shell-bash学习04读取输入、分隔符、流程控制

    读入输出 输入通常是通过stdin或参数传递给命令; 输出出现在stderr或stdout; 管道,过滤器,管道操作符: cmd1 | cmd2 | cmd3; //最后还有输出 ls | cat - ...

  8. android从资源文件中读取文件流显示

    在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样:代码区: private void doRaw(){ InputStream is = this ...

  9. ubuntu10.04编译内核不显示grub菜单解决

    问题描述:        ubuntu10.04 内核版本2.6.32.28编译内核之后版本2.6.37.6,系统在编译完内核之后,不显示grub菜单 参考资料:            http:// ...

随机推荐

  1. MySQL 表分区的几种方法和注意

    分区方法1:Hash分区 例子: create table thash(x int ,y int) partition by hash(x) partitions 4; 就这么一句话表就分好区了.下一 ...

  2. Windows phone 8.1 MessageBox 变了哦!

    using Windows.UI.Popups; public async void MessageBoxShow(string content, string caption) { MessageD ...

  3. Html小插件

    1.天气预报插件 效果图: 源代码: <iframe width="650" scrolling="no" height="60" f ...

  4. CCNA实验(6) -- VLAN & SPT

    交换机的作用主要有两个:1.维护CAM(ContextAddress Memory)表,该表是MAC地址和交换机端口的映射表2.根据CAM进行数据帧的转发 交换机对数据帧的处理有三种:1.Forwar ...

  5. 恢复sudo的权限的命令

    出错的原因:不小心给了/etc/的所有文件的777属性,出现了sudo 的错误. 1.pkexec chmod 0440 /etc/sudoers 2.pkexec chmod 0440 /etc/s ...

  6. svn+ssh

    According to official document, svn+ssh is supposed to be somehow faster than apache+dav_svn, howeve ...

  7. 微信jsapi支付的坑

    1.显示 redirect_uri 参数错误  因为手机支付是需要微信授权的,所以检查配置授权域名地址是否正确填写(登陆微信公众平台,地址:https://mp.weixin.qq.com/cgi-b ...

  8. Jsp指令有那些?

    <%@page language="java" contentType="text/html;charset=gb2312" session=" ...

  9. C3p0实践

    jar包 c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar mysql-connector-java-5.1.28-bin.jar 建立数据库 CRE ...

  10. 【转载】VC++中的图像类型转换--使用开源CxImage类库

    一.CxImage类库简介 这只是翻译了CxImage开源项目主页上的部分简介及简单使用. CxImage类库是一个优秀的图像操作类库.它可以快捷地存取.显示.转换各种图像.有的读者可能说,有那么多优 ...