Exemplo n.º 1
0
int do_attributes(GPU_Target* screen)
{
	GPU_LogError("do_attributes()\n");
	GPU_Image* image = GPU_LoadImage("data/small_test.png");
	if(image == NULL)
		return -1;
	
	int return_value = 0;
	
	float dt = 0.010f;
	
	Uint32 startTime = SDL_GetTicks();
	long frameCount = 0;
	
	int maxSprites = 50000;
	int numSprites = 101;
	
	// 2 pos floats per vertex, 2 texcoords, 4 color components
	int floats_per_vertex = 2 + 2 + 4;
	int floats_per_sprite = floats_per_vertex*4;
	float* sprite_values = (float*)malloc(sizeof(float)*maxSprites*floats_per_sprite);
	
	// Load attributes for the textured shader
	Uint32 program_object = 0;
	GPU_ActivateShaderProgram(program_object, NULL);
	// Disable the default shader's attributes (not a typical thing to do...)
	GPU_ShaderBlock block = {-1,-1,-1,GPU_GetUniformLocation(program_object, "gpu_ModelViewProjectionMatrix")};
	GPU_ActivateShaderProgram(program_object, &block);
	
	GPU_Attribute attributes[3] = {
	    GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_Vertex"), sprite_values, 
                                                    GPU_MakeAttributeFormat(2, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 0)),
        GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_TexCoord"), sprite_values, 
                                                    GPU_MakeAttributeFormat(2, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 2*sizeof(float))),
        GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_Color"), sprite_values, 
                                                    GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 4*sizeof(float)))
    };
	
	float* velx = (float*)malloc(sizeof(float)*maxSprites);
	float* vely = (float*)malloc(sizeof(float)*maxSprites);
	int i;
    int val_n = 0;
	for(i = 0; i < maxSprites; i++)
	{
	    float x = rand()%screen->w;
		float y = rand()%screen->h;
		sprite_values[val_n++] = x - image->w/2;
		sprite_values[val_n++] = y - image->h/2;
		
		sprite_values[val_n++] = 0;
		sprite_values[val_n++] = 0;
		
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		
		sprite_values[val_n++] = x + image->w/2;
		sprite_values[val_n++] = y - image->h/2;
		
		sprite_values[val_n++] = 1;
		sprite_values[val_n++] = 0;
		
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		
		sprite_values[val_n++] = x + image->w/2;
		sprite_values[val_n++] = y + image->h/2;
		
		sprite_values[val_n++] = 1;
		sprite_values[val_n++] = 1;
		
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		
		sprite_values[val_n++] = x - image->w/2;
		sprite_values[val_n++] = y + image->h/2;
		
		sprite_values[val_n++] = 0;
		sprite_values[val_n++] = 1;
		
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		sprite_values[val_n++] = rand()%101/100.0f;
		
		velx[i] = 10 + rand()%screen->w/10;
		vely[i] = 10 + rand()%screen->h/10;
		if(rand()%2)
            velx[i] = -velx[i];
		if(rand()%2)
            vely[i] = -vely[i];
	}
	
	
	Uint8 done = 0;
	SDL_Event event;
	while(!done)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
				done = 1;
			else if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_ESCAPE)
					done = 1;
				else if(event.key.keysym.sym == SDLK_SPACE)
                {
					done = 1;
					return_value = 1;
                }
				else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS)
				{
					if(numSprites < maxSprites)
						numSprites += 100;
                    GPU_LogError("Sprites: %d\n", numSprites);
                    frameCount = 0;
                    startTime = SDL_GetTicks();
				}
				else if(event.key.keysym.sym == SDLK_MINUS)
				{
					if(numSprites > 1)
						numSprites -= 100;
					if(numSprites < 1)
                        numSprites = 1;
                    GPU_LogError("Sprites: %d\n", numSprites);
                    frameCount = 0;
                    startTime = SDL_GetTicks();
				}
			}
		}
		
		GPU_Clear(screen);
		
		for(i = 0; i < numSprites; i++)
		{
		    val_n = floats_per_sprite*i;
		    float x = sprite_values[val_n] + image->w/2;
		    float y = sprite_values[val_n+1] + image->h/2;
		    
			x += velx[i]*dt;
			y += vely[i]*dt;
			if(x < 0)
			{
				x = 0;
				velx[i] = -velx[i];
			}
			else if(x > screen->w)
			{
				x = screen->w;
				velx[i] = -velx[i];
			}
			
			if(y < 0)
			{
				y = 0;
				vely[i] = -vely[i];
			}
			else if(y > screen->h)
			{
				y = screen->h;
				vely[i] = -vely[i];
			}
			
            sprite_values[val_n] = x - image->w/2;
            sprite_values[val_n+1] = y - image->h/2;
            val_n += floats_per_vertex;
            sprite_values[val_n] = x + image->w/2;
            sprite_values[val_n+1] = y - image->h/2;
            val_n += floats_per_vertex;
            sprite_values[val_n] = x + image->w/2;
            sprite_values[val_n+1] = y + image->h/2;
            val_n += floats_per_vertex;
            sprite_values[val_n] = x - image->w/2;
            sprite_values[val_n+1] = y + image->h/2;
		}
		
		//float color[4] = {0.5f, 0, 0, 1.0f};
		//GPU_SetAttributefv(attributes[2].location, 4, color);
		GPU_SetAttributeSource(numSprites*4, attributes[0]);
		GPU_SetAttributeSource(numSprites*4, attributes[1]);
		GPU_SetAttributeSource(numSprites*4, attributes[2]);
        GPU_BlitBatch(image, screen, numSprites, NULL, 0);
		
		GPU_Flip(screen);
		
		frameCount++;
		if(SDL_GetTicks() - startTime > 5000)
        {
			printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
			frameCount = 0;
			startTime = SDL_GetTicks();
        }
	}
	
	printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
	
	free(sprite_values);
	free(velx);
	free(vely);
	
	GPU_FreeImage(image);
	
	// Reset the default shader's block
	GPU_ActivateShaderProgram(screen->context->default_textured_shader_program, NULL);
	
	return return_value;
}
Exemplo n.º 2
0
int do_attributes(GPU_Target* screen)
{
    Uint32 startTime;
    long frameCount;
    Uint8 done;
    SDL_Event event;

    int return_value = 0;

    float dt = 0.010f;

    int max_vertices = 60000;
    int num_vertices = 303;

    float* velx = (float*)malloc(sizeof(float)*max_vertices/3);
    float* vely = (float*)malloc(sizeof(float)*max_vertices/3);
    int i;

    // 2 pos floats per vertex, 2 texcoords, 4 color components
    int floats_per_vertex = 2 + 2 + 4;
    float* vertex_values = (float*)malloc(sizeof(float)*max_vertices*floats_per_vertex);

    Uint32 program_object;
    GPU_Attribute attributes[3];

    GPU_Image* image = GPU_LoadImage("data/small_test.png");

    GPU_LogError("do_attributes()\n");
    if(image == NULL)
        return -1;

    startTime = SDL_GetTicks();
    frameCount = 0;

    fill_vertex_values(vertex_values, velx, vely, max_vertices, screen, image);

    // Load attributes for the textured shader
    program_object = 0;
    GPU_ActivateShaderProgram(program_object, NULL);
    // Disable the default shader's attributes (not a typical thing to do...)
    {
        GPU_ShaderBlock block = {-1,-1,-1,GPU_GetUniformLocation(program_object, "gpu_ModelViewProjectionMatrix")};
        GPU_ActivateShaderProgram(program_object, &block);
    }

    attributes[0] = GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_Vertex"), vertex_values,
                                      GPU_MakeAttributeFormat(2, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 0));
    attributes[1] = GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_TexCoord"), vertex_values,
                                      GPU_MakeAttributeFormat(2, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 2 * sizeof(float)));
    attributes[2] = GPU_MakeAttribute(GPU_GetAttributeLocation(program_object, "gpu_Color"), vertex_values,
                                      GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, floats_per_vertex*sizeof(float), 4 * sizeof(float)));


    done = 0;
    while(!done)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
                done = 1;
            else if(event.type == SDL_KEYDOWN)
            {
                if(event.key.keysym.sym == SDLK_ESCAPE)
                    done = 1;
                else if(event.key.keysym.sym == SDLK_SPACE)
                {
                    done = 1;
                    return_value = 4;
                }
                else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS)
                {
                    num_vertices += 300;
                    if(num_vertices > max_vertices)
                        num_vertices = max_vertices;
                    GPU_LogError("Vertices: %d\n", num_vertices);
                    frameCount = 0;
                    startTime = SDL_GetTicks();
                }
                else if(event.key.keysym.sym == SDLK_MINUS)
                {
                    if(num_vertices > 3)
                        num_vertices -= 300;
                    if(num_vertices < 3)
                        num_vertices = 3;
                    GPU_LogError("Vertices: %d\n", num_vertices);
                    frameCount = 0;
                    startTime = SDL_GetTicks();
                }
            }
        }

        GPU_Clear(screen);

        // FIXME: Can cause squishing when a vertex hits a wall...
        for(i = 0; i < num_vertices; i++)
        {
            int n = i/3;
            int val_n = floats_per_vertex*i;
            vertex_values[val_n] += velx[n]*dt;
            vertex_values[val_n+1] += vely[n]*dt;
            if(vertex_values[val_n] < 0)
            {
                vertex_values[val_n] = 0;
                velx[n] = -velx[n];
            }
            else if(vertex_values[val_n] > screen->w)
            {
                vertex_values[val_n] = screen->w;
                velx[n] = -velx[n];
            }

            if(vertex_values[val_n+1] < 0)
            {
                vertex_values[val_n+1] = 0;
                vely[n] = -vely[n];
            }
            else if(vertex_values[val_n+1] > screen->h)
            {
                vertex_values[val_n+1] = screen->h;
                vely[n] = -vely[n];
            }
        }

        GPU_SetAttributeSource(num_vertices, attributes[0]);
        GPU_SetAttributeSource(num_vertices, attributes[1]);
        GPU_SetAttributeSource(num_vertices, attributes[2]);
        GPU_TriangleBatch(image, screen, num_vertices, NULL, 0, NULL, GPU_NONE);

        GPU_Flip(screen);

        frameCount++;
        if(SDL_GetTicks() - startTime > 5000)
        {
            printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
            frameCount = 0;
            startTime = SDL_GetTicks();
        }
    }

    printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));

    free(vertex_values);
    free(velx);
    free(vely);

    GPU_FreeImage(image);

    // Reset the default shader's block
    GPU_ActivateShaderProgram(screen->context->default_textured_shader_program, NULL);

    return return_value;
}