1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| // 画图相关代码来自: // http://www.aaroncox.net/tutorials/2dtutorials/sdlshapes.html
#include "SDL.h" #include "SDL_gfxPrimitives.h"
const int WINDOW_WIDTH = 640; const int WINDOW_HEIGHT = 480; const char* WINDOW_TITLE = "SDL Test -- By Late Lee";
static SDL_Surface *g_screen;
enum color {BLACK, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, COLOR_MAX}; // Colors
// 颜色值:R G B A(alpha) static Uint32 g_colors[COLOR_MAX] ={ 0x000000ff, 0xff0000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff, 0xff00ffff, 0xffff00ff, 0xffffffff};
int initGraphic() { const SDL_VideoInfo *info; Uint8 video_bpp; Uint32 videoflags;
// Initialize SDL if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return 1; } atexit(SDL_Quit);
// Alpha blending doesn't work well at 8-bit color info = SDL_GetVideoInfo(); if ( info->vfmt->BitsPerPixel > 8 ) { video_bpp = info->vfmt->BitsPerPixel; } else { video_bpp = 16; } videoflags = SDL_SWSURFACE | SDL_DOUBLEBUF;
// Set 640x480 video mode if ( (g_screen=SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,video_bpp,videoflags)) == NULL ) { fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",640,480,SDL_GetError()); return 2; }
SDL_WM_SetCaption(WINDOW_TITLE, 0);
return 0; }
int drawGraphic() { //boxColor (g_screen, 0, 0, g_screen->w - 1, g_screen->h - 1, g_colors[RED]);
// 像素 //pixelRGBA(g_screen, 100, 100, 255, 0, 0, 255); // 水平线 hlineColor(g_screen, 10, 100, 10, g_colors[MAGENTA]); hlineColor(g_screen, 10, 100, 100, g_colors[MAGENTA]);
// 垂直线 vlineColor(g_screen, 10, 10, 100, g_colors[RED]); vlineColor(g_screen, 100, 10, 100, g_colors[RED]);
// 直线 lineColor(g_screen, 100, 100, 200, 10, g_colors[YELLOW]); lineRGBA(g_screen, 200, 10, 100, 50, 0x80, 0x11, 0xff, 0xff);
// 三角形 trigonColor(g_screen, 200, 10, 150, 100, 250, 100, g_colors[WHITE]);
filledTrigonColor(g_screen, 300, 10, 250, 100, 350, 100, g_colors[YELLOW]);
// 矩形 rectangleRGBA(g_screen, -10, 300, 100, 380, 0, 255, 0, 255);
// 填充 boxRGBA(g_screen, 210, 76, 325, 300, 255, 0, 0, 150);
// 圆 circleColor(g_screen, 500, 200, 100, g_colors[GREEN]);
// 椭圆 ellipseRGBA(g_screen, 600, 400, 50, 90, 255, 255, 0, 200); filledEllipseRGBA(g_screen, 600, 400, 25, 150, 0, 255, 0, 255);
// 多边形 short x[6] = { 350, 275, 300, 325, 350, 400 }; short y[6] = { 325, 325, 390, 390, 375, 300 };
polygonRGBA(g_screen, x, y, 6, 255, 255, 255, 155);
// 更新屏幕 SDL_Flip(g_screen); return 0; }
int main(int argc, char* argv[]) { int done = 0; SDL_Event event;
atexit(SDL_Quit); initGraphic(); drawGraphic(); while (!done) { /* Slow down polling */ SDL_Delay(100);
/* Check for events */ while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: done = 1; break; default: break; } } }
return 0; }
|