SDL 渲染纹理

  1 #include <windows.h>   // include important windows stuff
  2 
  3 #include "SDL.h"
  4 
  5 #include <cstdio>
  6 
  7 #pragma comment(lib,"SDL2.lib")
  8 #pragma comment(lib,"SDL2main.lib")
  9 
 10 #pragma warning(disable:4244) //全部关掉
 11 
 12 #define SCREEN_WIDTH 640            //窗口尺寸
 13 #define SCREEN_HEIGHT 480
 14 
 15                                  
 16 SDL_Window* gWindow = nullptr;//渲染的窗口
 17 
 18 
 19 SDL_Surface* gScreenSurface = nullptr;//窗口包含的表面
 20 
 21 SDL_Renderer* gRenderer = nullptr;
 22 
 23 
 24 SDL_Texture* gTexture = nullptr;//加载和显示到屏幕的图像
 25 
 26 
 27 UINT pixels[SCREEN_WIDTH * SCREEN_HEIGHT];
 28 
 29                                     
 30 bool init();//启动sdl,创建窗口
 31 
 32 void close();
 33 
 34 int Game_Main();
 35 
 36 
 37 void setPixels(int x, int y, UINT color);
 38 
 39 int main(int argc, char *argv[])
 40 {
 41     printf("Start Game!\n");
 42 
 43     if (!init())
 44     {
 45         printf("Failed to initialize!\n");
 46     }
 47     else
 48     {
 49         bool quit = false;
 50 
 51         SDL_Event e;
 52 
 53         while (!quit)
 54         {
 55             while (SDL_PollEvent(&e) != 0)
 56             {
 57                 if (e.type == SDL_QUIT)
 58                 {
 59                     quit = true;
 60                 }
 61             }
 63 
 64             Game_Main();
 66         }
 67     }
 68 
 69     close();
 70 
 71     return 0;
 72 }
 73 
 74 bool init()
 75 {
 76     bool success = true;
 77 
 78     if (SDL_Init(SDL_INIT_VIDEO) < 0)
 79     {
 80         printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
 81         success = false;
 82     }
 83     else
 84     {
 85         gWindow = SDL_CreateWindow("sdl test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
 86         if (gWindow == nullptr)
 87         {
 88             printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
 89             success = false;
 90         }
 91         else
 92         {
 93             gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
 94             if (gRenderer == nullptr)
 95             {
 96                 printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
 97                 success = false;
 98             }
 99             else
100             {
101                 gScreenSurface = SDL_GetWindowSurface(gWindow);
102 
103                 gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);
105             }
106         }
107     }
108 
109     return success;
110 }
111 
112 
113 void close()
114 {
115     SDL_DestroyTexture(gTexture);
116     gTexture = nullptr;
117 
118     SDL_DestroyRenderer(gRenderer);
119     SDL_DestroyWindow(gWindow);
120     gWindow = nullptr;
121     gRenderer = nullptr;
122 
123     SDL_Quit();
124 }
125 
126 void setPixels(int x, int y, UINT color) 
127 {
128     int index = (int)(SCREEN_WIDTH * y + x);
129 
130     pixels[index] = color;
131 }
132 
133 
134 //游戏循环
135 int Game_Main()
136 {
137     //清屏
138     SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
139     SDL_RenderClear(gRenderer);
140 
141     //清空像素
142     memset(pixels, 0xFF0000, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(Uint32));
143 
144     //设置像素点
145     setPixels(10, 10, 0xFF0000);
146     setPixels(20, 20, 0x00FF00);
147 
148     SDL_UpdateTexture(gTexture, NULL, pixels, 640 * sizeof(Uint32));
149 
150     //渲染贴图到屏幕
151     SDL_RenderCopy(gRenderer, gTexture, nullptr, nullptr);
152 
153     //显示
154     SDL_RenderPresent(gRenderer);
155 
156     return 1;
157 }

 

posted @ 2016-04-28 17:06  luozhifu  阅读(745)  评论(0编辑  收藏  举报