this repo has no description
at master 245 lines 7.5 kB view raw
1#include "SDL.h" 2#include "SDL_gpu.h" 3#include <math.h> 4#include "common.h" 5#include "compat.h" 6 7 8 9void set_pixel(SDL_Surface* surface, int x, int y, Uint32 color) 10{ 11 int bpp; 12 Uint8* bits; 13 14 if(surface == NULL || x < 0 || y < 0 || x >= surface->w || y >= surface->h) 15 return; 16 17 bpp = surface->format->BytesPerPixel; 18 bits = ((Uint8 *)surface->pixels) + y*surface->pitch + x*bpp; 19 20 /* Set the pixel */ 21 switch(bpp) 22 { 23 case 1: 24 *((Uint8 *)(bits)) = (Uint8)color; 25 break; 26 case 2: 27 *((Uint16 *)(bits)) = (Uint16)color; 28 break; 29 case 3: { /* Format/endian independent */ 30 Uint8 r,g,b; 31 r = (color >> surface->format->Rshift) & 0xFF; 32 g = (color >> surface->format->Gshift) & 0xFF; 33 b = (color >> surface->format->Bshift) & 0xFF; 34 *((bits)+surface->format->Rshift/8) = r; 35 *((bits)+surface->format->Gshift/8) = g; 36 *((bits)+surface->format->Bshift/8) = b; 37 } 38 break; 39 case 4: 40 *((Uint32 *)(bits)) = (Uint32)color; 41 break; 42 } 43} 44 45 46int main(int argc, char* argv[]) 47{ 48 GPU_Target* screen; 49 50 printRenderers(); 51 52 screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); 53 if(screen == NULL) 54 return 1; 55 56 printCurrentRenderer(); 57 58 { 59 Uint32 startTime; 60 long frameCount; 61 Uint8 done; 62 SDL_Event event; 63 64 Uint32 rmask, gmask, bmask, amask; 65 int num_surfaces = 4; 66 SDL_Surface* surfaces[4]; 67 int i; 68 int current_surface = 0; 69 70 SDL_Color red = {255, 0, 0, 255}; 71 SDL_Color white = {255, 255, 255, 255}; 72 73 GPU_Rect surface_rect = {50, 70, 100, 100}; 74 75 SDL_Color border_color = {255, 255, 255, 255}; 76 SDL_Color surface_rect_color = {0, 255, 0, 255}; 77 78 const Uint8* keystates = SDL_GetKeyState(NULL); 79 SDL_Keymod kmod; 80 81 int mx, my; 82 Uint32 mouse_state; 83 84 GPU_Image* image; 85 GPU_Image* selection_image; 86 87 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 88 rmask = 0xff000000; 89 gmask = 0x00ff0000; 90 bmask = 0x0000ff00; 91 amask = 0x000000ff; 92 #else 93 rmask = 0x000000ff; 94 gmask = 0x0000ff00; 95 bmask = 0x00ff0000; 96 amask = 0xff000000; 97 #endif 98 99 // Create surfaces which will replace the main image 100 surfaces[0] = GPU_LoadSurface("data/test.bmp"); 101 if(surfaces[0] == NULL) 102 return 2; 103 104 for(i = 1; i < num_surfaces; ++i) 105 { 106 surfaces[i] = SDL_CreateRGBSurface(SDL_SWSURFACE, 50 * (i+1), 40 * (i+1), 32, 107 rmask, gmask, bmask, amask); 108 if(surfaces[i] == NULL) 109 return 3; 110 111 SDL_FillRect(surfaces[i], NULL, SDL_MapRGBA(surfaces[i]->format, rand()%256, rand()%256, rand()%256, 255)); 112 } 113 114 image = GPU_CopyImageFromSurface(surfaces[0]); 115 if(image == NULL) 116 return 4; 117 118 selection_image = GPU_CopyImageFromSurface(surfaces[0]); 119 if(selection_image == NULL) 120 return 5; 121 122 // Fade out the selection image 123 GPU_SetColor(selection_image, GPU_MakeColor(255, 255, 255, 100)); 124 125 if(!GPU_LoadTarget(image)) 126 return 6; 127 128 startTime = SDL_GetTicks(); 129 frameCount = 0; 130 131 done = 0; 132 while(!done) 133 { 134 while(SDL_PollEvent(&event)) 135 { 136 if(event.type == SDL_QUIT) 137 done = 1; 138 else if(event.type == SDL_KEYDOWN) 139 { 140 if(event.key.keysym.sym == SDLK_ESCAPE) 141 done = 1; 142 if(event.key.keysym.sym == SDLK_r) 143 { 144 GPU_Clear(image->target); 145 } 146 if(event.key.keysym.sym == SDLK_SPACE) 147 { 148 // Replace images with the next surface 149 current_surface++; 150 if(current_surface >= num_surfaces) 151 current_surface = 0; 152 153 GPU_ReplaceImage(image, surfaces[current_surface], NULL); 154 GPU_ReplaceImage(selection_image, surfaces[current_surface], NULL); 155 } 156 if(event.key.keysym.sym == SDLK_RETURN) 157 { 158 // Replace the main image with a section of the current source surface 159 GPU_ReplaceImage(image, surfaces[current_surface], &surface_rect); 160 } 161 } 162 } 163 164 kmod = SDL_GetModState(); 165 166 // Adjust source rectangle 167 if(keystates[KEY_UP]) 168 { 169 if(kmod & KMOD_SHIFT) 170 surface_rect.h -= 1; 171 else 172 surface_rect.y -= 1; 173 } 174 if(keystates[KEY_DOWN]) 175 { 176 if(kmod & KMOD_SHIFT) 177 surface_rect.h += 1; 178 else 179 surface_rect.y += 1; 180 } 181 if(keystates[KEY_LEFT]) 182 { 183 if(kmod & KMOD_SHIFT) 184 surface_rect.w -= 1; 185 else 186 surface_rect.x -= 1; 187 } 188 if(keystates[KEY_RIGHT]) 189 { 190 if(kmod & KMOD_SHIFT) 191 surface_rect.w += 1; 192 else 193 surface_rect.x += 1; 194 } 195 196 // Scribble some pixels on the current main image 197 mouse_state = SDL_GetMouseState(&mx, &my); 198 if(mouse_state & SDL_BUTTON_LMASK) 199 { 200 GPU_Pixel(image->target, mx + image->w/2 - screen->w/2, my + image->h/2 - screen->h/2, red); 201 } 202 if(mouse_state & SDL_BUTTON_RMASK) 203 { 204 GPU_Pixel(image->target, mx + image->w/2 - screen->w/2, my + image->h/2 - screen->h/2, white); 205 } 206 207 GPU_Clear(screen); 208 209 // Draw selection image 210 GPU_Blit(selection_image, NULL, screen, selection_image->w/2, selection_image->h/2); 211 GPU_Rectangle(screen, 0, 0, selection_image->w, selection_image->h, border_color); 212 213 // Draw source region 214 GPU_Rectangle(screen, surface_rect.x, surface_rect.y, 215 surface_rect.x + surface_rect.w, surface_rect.y + surface_rect.h, surface_rect_color); 216 217 // Draw replaceable main image 218 GPU_Blit(image, NULL, screen, screen->w/2, screen->h/2); 219 GPU_Rectangle(screen, screen->w/2 - image->w/2, screen->h/2 - image->h/2, 220 screen->w/2 + image->w/2, screen->h/2 + image->h/2, border_color); 221 222 GPU_Flip(screen); 223 224 frameCount++; 225 if(frameCount%500 == 0) 226 printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 227 } 228 229 printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 230 231 GPU_FreeImage(selection_image); 232 GPU_FreeImage(image); 233 234 for(i = 0; i < num_surfaces; ++i) 235 { 236 SDL_FreeSurface(surfaces[i]); 237 } 238 } 239 240 GPU_Quit(); 241 242 return 0; 243} 244 245