sdl

// sdltest1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <SDL.h>

#include <stdlib.h>
#include <windows.h>



void draw2(){
    SDL_Window *window;
    SDL_Renderer *renderer =NULL;
    SDL_Init(SDL_INIT_VIDEO);
    //window = SDL_CreateWindowFrom((void *)(gwnd));
    window = SDL_CreateWindow("窗口标题",300,300,300,300,SDL_WINDOW_SHOWN| SDL_WINDOW_RESIZABLE);

    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
    if (window) {
        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        if (!renderer) {
            renderer = SDL_CreateRenderer(window, -1, 0);
        }
        if (renderer) {
        }
    }

    SDL_Texture *texture = NULL; // 纹理
    // 创建纹理(第二个参数是指明像素格式,可以是YUV或RGB,第3个参数是指明纹理类型,可以是stream(流)或target(一般类型))
    texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 200, 150);
    if (!texture)
    {
        printf("纹理创建失败!\n");
        return;
    }
    SDL_Rect rect; // 绘制的矩形

    for (int i = 0; i < 10;i++)
    {

        // 随机设置矩形位置
        rect.x = rand() % 600;
        rect.y = rand() % 440;

        // 对纹理进行渲染
        SDL_SetRenderTarget(renderer, texture); // 改变渲染目标为纹理
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // 设置纹理颜色(颜色为RGBA)
        SDL_RenderClear(renderer); // 清空渲染器
        // 绘制矩形并给矩形填充颜色
        SDL_RenderDrawRect(renderer, &rect); // 绘制矩形
        SDL_SetRenderDrawColor(renderer, rand() % 256, rand() % 256, rand() % 256, 255); // 给矩形设置随机色
        SDL_RenderFillRect(renderer, &rect); // 填充
        // 恢复成默认渲染目标
        SDL_SetRenderTarget(renderer, NULL);
        // 将目标输出到显卡
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer); // 显卡计算完后进行显示
        Sleep(300);
    } 
    if (window) // 销毁window
    {
        SDL_DestroyWindow(window);
    }
    if (renderer) // 销毁渲染器
    {
        SDL_DestroyRenderer(renderer);
    }
    if (texture) // 销毁纹理
    {
        SDL_DestroyTexture(texture);
    }
    SDL_Quit();
}

int _tmain(int argc, _TCHAR* argv[])
{
    draw2();
    return -1;
    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Init(SDL_INIT_VIDEO);
    
    //window = SDL_CreateWindowFrom((void *)(gwnd));
        window = SDL_CreateWindow("窗口标题",300,300,300,300,SDL_WINDOW_SHOWN| SDL_WINDOW_RESIZABLE);

    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
    if (window) {
        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        if (!renderer) {
            renderer = SDL_CreateRenderer(window, -1, 0);
        }
        if (renderer) {
            //if (!SDL_GetRendererInfo(renderer, &renderer_info))
        }
    }

    SDL_Surface*surface = SDL_GetWindowSurface(window);
    SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0xee, 0xFF, 0xFF));

    //更新surface
    SDL_UpdateWindowSurface(window);

    //延迟两秒
    SDL_Delay(2000);
}

int _tmain1(int argc, _TCHAR* argv[])
{
    SDL_Window *window = NULL; // 窗口
    SDL_Renderer *render = NULL;// 渲染器
    SDL_Texture *texture = NULL; // 纹理
    SDL_Event event; // 监听事件
    int quit = 0; // 是否退出
    int win_w = 640; // 窗口宽度
    int win_h = 480; // 窗口高度
    SDL_Rect rect; // 绘制的矩形
    rect.w = 40;
    rect.h = 40;
    SDL_Init(SDL_INIT_VIDEO);

    // 创建窗口,参数分别是窗口的标题,x,y,w,h,最后一个参数是一些flag(SDL_WINDOW_SHOWN表示将窗口显示出来)
    window = SDL_CreateWindow("窗口标题",300,300,win_w,win_h,SDL_WINDOW_SHOWN| SDL_WINDOW_RESIZABLE);
    if (!window)
    {
        printf("创建窗口失败\n");
      //  goto __EXIT;
        return -1;
    }

    // 窗口渲染
    // 创建渲染器
    render = SDL_CreateRenderer(window,-1,0);// 创建渲染器
    if (!render)
    {
        printf("创渲染器口失败\n");
      //  goto __DESTROY;
        return -1;
    }


    SDL_RenderClear(render);

    SDL_Surface*surface=SDL_LoadBMP("3dcheck.bmp");

    SDL_Rect box={0, 0, surface->w*2, surface->h*2};

    texture=SDL_CreateTextureFromSurface(render, surface);

    

        SDL_RenderCopy(render, texture, NULL, &box);
        SDL_RenderPresent(render);

    SDL_FreeSurface(surface);


while (!quit) {
    SDL_WaitEvent(&event);
    switch (event.type) {
        case SDL_QUIT://退出事件
            SDL_Log("quit");
            quit = 1;
            break;
        default:
            SDL_Log("event type:%d", event.type);
    }
}

__DESTROY:
    if (window) // 销毁window
    {
        SDL_DestroyWindow(window);
    }
    if (render) // 销毁渲染器
    {
        SDL_DestroyRenderer(render);
    }
    if (texture) // 销毁纹理
    {
        SDL_DestroyTexture(texture);
    }
    

__EXIT:
    // 退出
    SDL_Quit();
    return 0;
}

 

上一篇:electron-vue打包后的项目控制台没有日志的解决办法


下一篇:day14