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
|
#include "main.h"
int main(int argv, char **args)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("Error");
return 0;
}
const int w = 680;
const int h = 480;
SDL_Window *window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
w, h,
0);
if (!window)
{
std::cout << "Failed to create window\n";
return -1;
}
/*SDL_Surface *window_surface = SDL_GetWindowSurface(window);
if (!window_surface)
{
std::cout << "Failed to get the surface from the window\n";
return -1;
}
*/
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, w, h);
Uint32 pixels[w*h];
Point points[pointsN];
for (size_t i = 0; i < pointsN; i++)
{
Point newpoint;
newpoint.x = rand() % w;
newpoint.y = rand() % h;
newpoint.color.color.r = rand() % 255;
newpoint.color.color.g = rand() % 255;
newpoint.color.color.b = rand() % 255;
newpoint.color.color.a = 255;
points[i] = newpoint;
}
SDL_Event e;
bool keep_window_open = true;
unsigned int frames = 0;
Uint64 start = SDL_GetPerformanceCounter();
//bool mouseDown = false;
while (keep_window_open)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
while (SDL_PollEvent(&e) > 0)
{
switch (e.type)
{
case SDL_QUIT:
keep_window_open = false;
break;
}
}
for (size_t y = 0; y < h; y++)
{
for (size_t x = 0; x < w; x++)
{
/*const unsigned int offset = (texWidth * 4 * y) + x * 4;
pixels[offset + 0] = rand() % 256; // b
pixels[offset + 1] = rand() % 256; // g
pixels[offset + 2] = rand() % 256; // r
pixels[offset + 3] = SDL_ALPHA_OPAQUE; // a
*/
pixels[(w * y) + x] = nearestPoint(points, x, y).color.color_int;
}
}
for (size_t i = 0; i < pointsN; i++)
{
points[i].x += (rand() % 50 + 1) - 25;
points[i].y += (rand() % 50 + 1) - 25;
}
SDL_UpdateTexture(
texture,
NULL,
pixels,
w * sizeof(Uint32));
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
frames++;
const Uint64 end = SDL_GetPerformanceCounter();
const static Uint64 freq = SDL_GetPerformanceFrequency();
const double seconds = (end - start) / static_cast<double>(freq);
printf("FPS: %f\n", frames / seconds);
/*SDL_UpdateTexture(texture, NULL, pixels, w * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
*/
//SDL_UpdateWindowSurface(window);
}
//delete[] pixels;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
|