Lv.的博客

SDL2.0的几何图形绘画

通过SDL_Window、SDL_Renderer、SDL_Texture三者实现了简单的几何图形绘画。

包括了SDL_RenderDrawPoint、SDL_RenderFillRect、SDL_RenderDrawLine、SDL_SetRenderDrawColor等。

具体看代码吧(VS2012运行):

复制代码
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <time.h>
  4 #include <SDL2\SDL.h>
  5 #include <SDL2\SDL_image.h>
  6 #include <SDL2\ex\SDLex.h>
  7 
  8 SDL_Window *sdlWindow = NULL;
  9 SDL_Renderer *sdlRender = NULL;
 10 SDL_Texture *sdlTexture = NULL;
 11 SDL srcRect;
 12 int w = 500;
 13 int h = 500;
 14 
 15 void DrawCircle(SDL_Renderer *ren,int radius){
 16     int st=clock(),tx=0,ty=radius,d=3-(radius<<1),x=radius,y=radius;
 17     while(tx<ty){
 18         for (int i=x-ty;i<=x+ty;++i){
 19             SDL_RenderDrawPoint(ren,i,y-tx);
 20             if (tx)
 21                 SDL_RenderDrawPoint(ren,i,y+tx);
 22         }
 23         if (d<0)
 24             d+=(tx<<2)+6;
 25         else{
 26             for (int i=x-tx;i<=x+tx;++i){
 27                 SDL_RenderDrawPoint(ren,i,y-ty);
 28                 SDL_RenderDrawPoint(ren,i,y+ty);
 29             }
 30             d+=((tx - ty)<<2)+10,ty--;
 31         }
 32         tx++;
 33     }
 34     if (tx==ty)
 35         for (int i=x-ty;i<=x+ty;++i){
 36                 SDL_RenderDrawPoint(ren,i,y-tx);
 37                 SDL_RenderDrawPoint(ren,i,y+tx);
 38         }
 39     int en=clock();
 40 }
 41 
 42 bool InitView(int width, int height, const char *iconName)
 43 {
 44     //初始化窗体
 45     SDL_Init(SDL_INIT_VIDEO);
 46 
 47     sdlWindow = SDL_CreateWindow(
 48         "The First SDL Program", 
 49         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 
 50         SDL_WINDOW_RESIZABLE);
 51     if (sdlWindow == NULL) return false;
 52 
 53     //加载窗体图标
 54     SDL_Surface *iconSurface = IMG_Load(iconName);
 55     if (iconSurface == NULL) return false;
 56 
 57     SDL_SetWindowIcon(sdlWindow, iconSurface);
 58 
 59     return true;
 60 }
 61 
 62 bool InitDraw()
 63 {
 64     //加载渲染器
 65     sdlRender = SDL_CreateRenderer(sdlWindow, -1, 0);
 66     if (sdlRender == NULL) return false;
 67 
 68     sdlTexture = SDL_CreateTexture(sdlRender, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
 69     if (sdlTexture == NULL) return false;
 70     //SDL_SetTextureBlendMode(sdlTexture, SDL_BLENDMODE_BLEND);
 71     //SDL_SetRenderTarget(sdlRender, sdlTexture);
 72 
 73     return true;
 74 }
 75 
 76 void UpdateDraw()
 77 {
 78     //设置背景颜色
 79     SDL_SetRenderDrawColor(sdlRender, 255, 153, 153, 0xFF);
 80     SDL_RenderClear(sdlRender);
 81     
 82     //左眼
 83     SDL_SetRenderDrawColor(sdlRender, 0x00, 0xFF, 0xFF, 0xFF);
 84     srcRect = SDLMake(w/7*2, h/7, w/8, h/8);
 85     SDL_RenderFillRect(sdlRender, &srcRect);
 86     
 87     //左眉毛
 88     SDL_SetRenderDrawColor(sdlRender, 0x00, 0x00, 0x00, 0xFF);
 89     SDL_RenderDrawLine(sdlRender, srcRect.x, srcRect.y - 10, SDLMaxX(srcRect), srcRect.y - 10);
 90     
 91     //右眼
 92     SDL_SetRenderDrawColor(sdlRender, 0xFF, 0x00, 0xFF, 0xFF);
 93     srcRect = SDLMake(w/7*4, srcRect.y, srcRect.w, srcRect.h);
 94     SDL_RenderDrawRect(sdlRender, &srcRect);
 95     
 96     //右眉毛
 97     SDL_SetRenderDrawColor(sdlRender, 0x00, 0x00, 0x00, 0xFF);
 98     SDL_RenderDrawLine(sdlRender, srcRect.x, srcRect.y - 10, SDLMaxX(srcRect), srcRect.y - 10);
 99     
100     //嘴巴
101     SDL_SetRenderDrawColor(sdlRender, 0xFF, 0xFF, 0x00, 0xFF);
102     srcRect = SDLMake(w/8*3, h/7*3, w/8*2, h/8);
103     SDL_RenderFillRect(sdlRender, &srcRect);
104     
105     SDL_SetRenderDrawColor(sdlRender, 0xFF, 0x00, 0x00, 0xFF);
106     DrawCircle(sdlRender, 10);
107     
108     SDL_RenderPresent(sdlRender);
109 }
110 
111 void Quit(int code)
112 {
113     const char *errMsg = SDL_GetError();
114     if (errMsg && strlen(errMsg)) {
115         SDL_Log("Error : %s\n", errMsg);
116     }
117 
118     //销毁窗口、渲染器、纹理
119     if (sdlWindow) SDL_DestroyWindow(sdlWindow);
120     if (sdlRender) SDL_DestroyRenderer(sdlRender);
121     if (sdlTexture) SDL_DestroyTexture(sdlTexture);
122     SDL_Quit();
123     exit(code);
124 }
125 
126 void HandleKeyEvent(const SDL_Keysym* keysym)
127 {
128     int key = keysym->sym;
129     switch(key)
130     {
131     case SDLK_ESCAPE:
132         Quit(0);
133         break;
134     case SDLK_SPACE:
135         break;
136     case SDLK_UP:
137     case SDLK_DOWN:
138     case SDLK_LEFT:
139     case SDLK_RIGHT:
140         int x, y;
141         SDL_GetWindowPosition(sdlWindow, &x, &y);
142         x = (key == SDLK_LEFT ? x-2 : (key == SDLK_RIGHT ? x+2 : x));
143         y = (key == SDLK_UP ? y-2 : (key == SDLK_DOWN ? y+2 : y));
144         SDL_SetWindowPosition(sdlWindow, x, y);
145         SDL_Log("x=%d, y=%d\n", x, y);
146         break;
147     case SDLK_KP_PLUS:
148     case SDLK_KP_MINUS:
149         w = (key == SDLK_KP_PLUS ? w+2 : w-2);
150         h = (key == SDLK_KP_PLUS ? h+2 : h-2);
151         SDL_SetWindowSize(sdlWindow, w, h);
152         SDL_Log("w=%d, h=%d\n", w, h);
153         break;
154     default:
155         break;
156     }
157 }
158 
159 void HandleEvents()
160 {
161     //Our SDL event placeholder.
162     SDL_Event event;
163     //Grab all the events off the queue.
164     while(SDL_PollEvent(&event)) {
165         switch(event.type) {
166         case SDL_KEYDOWN:
167             //Handle key Event
168             HandleKeyEvent(&event.key.keysym);
169             break;
170         case SDL_QUIT:
171             //Handle quit requests (like Ctrl-c).
172             Quit(0);
173             break;
174         }
175     }
176 }
177 
178 int main(int argc, char* argv[])
179 {
180     printf("可以通过↑↓←→+ -按键控制移动和大小\n");
181     if (InitView(w, h, "yp.ico") == false) {
182         SDL_Log("sdlWindow is null @_@\n");
183         Quit(0);
184         return -1;
185     }
186 
187     if (InitDraw() == false) {
188         SDL_Log("Init Fail @_@\n");
189         Quit(0);
190         return -1;
191     }
192     
193     //配置客户区大小
194     SDL_QueryTexture(sdlTexture,NULL, NULL, &w, &h);
195     SDL_SetWindowSize(sdlWindow, w + 2, h);
196     SDL_Log("w=%d, h=%d\n", w, h);
197     
198     while (1) {
199         HandleEvents();
200         UpdateDraw();
201     }
202     
203     SDL_DestroyWindow(sdlWindow);
204     SDL_Quit();
205     return 0;
206 }
复制代码

效果图:

 

posted @ 2020-03-14 18:17  Avatarx  阅读(477)  评论(0编辑  收藏  举报