while (!done) { while (SDL_PollEvent(&event)) // { swtich(event.type) { case SDL_QUIT: // done = true; } } }
while (!done) { SDL_WaitEvent(&event); // }
case SDL_MOUSEBUTTONDOWN: { if (event.button.button = SDL_BUTTON_LEFT) { int x = event.button.x, y = event.button.y; // if (x < 30 * maps && y < 30 * maps) AddColor(map[x / 30][y / 30]); DrawStep(); // , . SDL_Flip(screen); } break; }
case SDL_KEYDOWN: { switch(event.key.keysym.sym) { case SDLK_ESCAPE: // Esc { done = true; break; } case SDLK_b: // B { AddColor(COLOR_BLUE), DrawStep(), SDL_Flip(screen); // COLOR_BLUE — define break; } case SDLK_g: { AddColor(COLOR_GREEN), DrawStep(), SDL_Flip(screen); break; } case SDLK_o: { AddColor(COLOR_ORANGE), DrawStep(), SDL_Flip(screen); break; } case SDLK_r: { AddColor(COLOR_RED), DrawStep(), SDL_Flip(screen); break; } case SDLK_v: { AddColor(COLOR_VIOLETT), DrawStep(), SDL_Flip(screen); break; } case SDLK_y: { AddColor(COLOR_YELLOW), DrawStep(), SDL_Flip(screen); } } break; }
SDL_SetColorKey(img, SDL_SRCCOLORKEY, SDL_MapRGB(img->format,255, 0, 255));
TTF_Init();
void WriteText(int x, int y, char text[100], int sz, int r, int g, int b) { SDL_Color clr; // . 4 — R, G, B A, . clr.r = r; clr.g = g; // clr.b = b; TTF_Font * fnt = TTF_OpenFont("courier.ttf", sz); // sz SDL_Rect dest; dest.x = x; dest.y = y; SDL_Surface * TextSurface = TTF_RenderText_Blended(fnt, text, clr); // SDL_BlitSurface(TextSurface, NULL, screen, &dest); SDL_FreeSurface(TextSurface); // TTF_CloseFont(fnt); // }
void CleanImage(int x, int y, int w, int h) { SDL_Rect desc; desc.x = x; desc.y = y; desc.w = w; desc.h = h; SDL_FillRect(screen, &desc, 0); // ( 0) } void DrawStep() { CleanImage(430, 50, 150, 100); for (int i = 0; i < 100; i++) tmpch[i] = 0; if (FinishGame()) // { CleanImage(430, 50, 150, 20); for (int i = 0; i < 100; i++) tmpch[i] = 0; if (step <= 25) // 25 sprintf(tmpch, "You win!"), WriteText(430, 50, tmpch, 20, 0, 255, 0); // - (0, 255, 0) else sprintf(tmpch, "Not bad!"), WriteText(430, 50, tmpch, 20, 255, 255, 0); for (int i = 0; i < 100; i++) tmpch[i] = 0; sprintf(tmpch, "Result:%d", step); WriteText(430, 80, tmpch, 20, 255, 255, 0); step = 0; GenMap(); // , DrawMap(); } else { sprintf(tmpch, "%d%s", step, "/25"); if (step <= 25) WriteText(430, 50, tmpch, 20, 0, 255, 0); // , 25 else WriteText(430, 50, tmpch, 20, 255, 0, 0); } }
Source: https://habr.com/ru/post/135035/
All Articles