显示图片 test_bmp.c

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "SDL2/SDL.h"
#include <windows.h>

typedef struct Sprite
{
    SDL_Texture* texture;
    Uint16 w;
    Uint16 h;
} Sprite;

/* Adapted from SDL's testspriteminimal.c */
Sprite LoadSprite(const char* file, SDL_Renderer* renderer)
{
    Sprite spr;
    spr.texture = NULL;
    spr.w = 0;
    spr.h = 0;

    SDL_Surface* temp;

    /* Load the sprite image */
    temp = SDL_LoadBMP(file);
    if (temp == NULL)
    {
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
        return spr;
    }
    spr.w = temp->w/2;
    spr.h = temp->h/2;

    /* Create texture from the image */
    spr.texture = SDL_CreateTextureFromSurface(renderer, temp);
    if (!spr.texture) {
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
        SDL_FreeSurface(temp);
        return spr;
    }
    SDL_FreeSurface(temp);

    return spr;
}

void draw(SDL_Window* window, SDL_Renderer* renderer, const Sprite sprite)
{
    int w, h;
    SDL_GetWindowSize(window, &w, &h);
    SDL_Rect destRect = {w/2 - sprite.w/2, h/2 - sprite.h/2, sprite.w, sprite.h};
    /* Blit the sprite onto the screen */
    SDL_RenderCopy(renderer, sprite.texture, NULL, &destRect);
}

int main(int argc, char *argv[])
{
    SDL_Window *window;
    SDL_Renderer *renderer;

        window = SDL_CreateWindow("SDL测试 - 风的影子", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 480, 720, SDL_WINDOW_SHOWN);
//  renderer = SDL_GetRenderer(window);
renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
 if(renderer==NULL){
     printf("renderer is null");
 }
    Sprite sprite = LoadSprite("sprite.bmp", renderer);
    if(sprite.texture == NULL){
        printf("texture is null");
        exit(2);
    }

    /* Main render loop */
    Uint8 done = 0;
    SDL_Event event;
    while(!done)
    {
        /* Check for events */
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
            {
                done = 1;
            }
        }

        /* Draw a gray background */
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
        SDL_RenderClear(renderer);

        draw(window, renderer, sprite);

        /* Update the screen! */
        SDL_RenderPresent(renderer);

        SDL_Delay(10);
    }

    exit(0);
}

清屏 fillrect.c

//最简单的SDL程序
//2015.8.11
//程序功能:显示一个640*480大小的窗口,窗口内容为绿色
//
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>

//Screen dimension constants  屏幕宽高设置
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char *args[])
{
    //The window we'll be rendering to
    SDL_Window *window = NULL;

    //The surface contained by the window
    SDL_Surface *screenSurface = NULL;

    //Initialize SDL  初始化SDL视频子系统
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 0;
    }

    //Create window  创建窗口
    window = SDL_CreateWindow("Hello SDL",             //窗口标题
                              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());
        return 0;
    }

    //Get window surface
    screenSurface = SDL_GetWindowSurface(window);

    /* Main render loop */
    Uint8 done = 0;
    SDL_Event event;
    while (!done)
    {
        /* Check for events */
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
            {
                done = 1;
            }
        }

        //Fill the surface green   填充颜色为绿色
        SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0xff, 0x00));

        //Update the surface
        SDL_UpdateWindowSurface(window);

        //Wait two seconds 延时2000毫秒
        SDL_Delay(10);

    }
    //Destroy window
    SDL_DestroyWindow(window);
    //Quit SDL subsystems
    SDL_Quit();

    return 1;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注