コード例 #1
0
ファイル: main.c プロジェクト: 07151129/sdl-gpu
int main(int argc, char* argv[])
{
	GPU_Target* screen;

	printRenderers();
	
	screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
		return -1;
	
	printCurrentRenderer();
	
	{
		Uint32 startTime;
		long frameCount;
		Uint8 done;
		SDL_Event event;
		
		GPU_Image* image;
        #define MAX_SPRITES 100
        int numSprites;
        float positions[2*MAX_SPRITES];
        float colors[4*4*MAX_SPRITES];
        float expanded_colors[4*MAX_SPRITES];
        float src_rects[4*MAX_SPRITES];
        Uint32 v, f, p;
        GPU_ShaderBlock block;
        Uint8 shader_index;
        int i;
        SDL_Color color = {255, 255, 255, 255};
        SDL_Color red = {255, 0, 0, 255};
        SDL_Color green = {0, 255, 0, 255};
        SDL_Color blue = {0, 0, 255, 255};
        GPU_Rect src_rect;
        
        int mx, my;
        Uint32 mouse_state;
        
        image = GPU_LoadImage("data/happy_50x50.bmp");
        if(image == NULL)
            return -1;
        
        numSprites = 0;
        
        color_attr.format = GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, 4*sizeof(float), 0);
        color_attr.format.is_per_sprite = 0;
        color_attr.values = colors;
        
        
        block = load_shaders(&v, &f, &p);
        
        shader_index = 1;
        set_shader(p, &block);
        
        startTime = SDL_GetTicks();
        frameCount = 0;
        
        src_rect.x = 0;
        src_rect.y = 0;
        src_rect.w = image->w;
        src_rect.h = image->h;
        
        add_sprite(positions, colors, expanded_colors, src_rects, &numSprites, color, src_rect);
        
        done = 0;
        while(!done)
        {
            while(SDL_PollEvent(&event))
            {
                if(event.type == SDL_QUIT)
                    done = 1;
                else if(event.type == SDL_MOUSEBUTTONDOWN)
                {
                    if(event.button.x <= 150 && event.button.y <= 150)
                    {
                        if(event.button.button == SDL_BUTTON_LEFT)
                        {
                            float dx = event.button.x/3 - src_rect.x;
                            float dy = event.button.y/3 - src_rect.y;
                            src_rect.x = event.button.x/3;
                            src_rect.y = event.button.y/3;
                            src_rect.w -= dx;
                            src_rect.h -= dy;
                        }
                        else if(event.button.button == SDL_BUTTON_RIGHT)
                        {
                            src_rect.w = event.button.x/3 - src_rect.x;
                            src_rect.h = event.button.y/3 - src_rect.y;
                        }
                    }
                }
                else if(event.type == SDL_KEYDOWN)
                {
                    if(event.key.keysym.sym == SDLK_ESCAPE)
                        done = 1;
                    else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS)
                    {
                        if(numSprites < MAX_SPRITES)
                            add_sprite(positions, colors, expanded_colors, src_rects, &numSprites, color, src_rect);
                    }
                    else if(event.key.keysym.sym == SDLK_MINUS)
                    {
                        if(numSprites > 0)
                            numSprites--;
                    }
                    else if(event.key.keysym.sym == SDLK_SPACE)
                    {
                        shader_index++;
                        shader_index %= 2;
                        if(shader_index == 0)
                            set_shader(0, NULL);
                        else if(shader_index == 1)
                            set_shader(p, &block);
                    }
                    else if(event.key.keysym.sym == SDLK_RETURN)
                    {
                        use_color_expansion = !use_color_expansion;
                        if(use_color_expansion)
                        {
                            GPU_LogError("Using attribute expansion.\n");
                            color_attr.format.is_per_sprite = 1;
                            color_attr.values = expanded_colors;
                        }
                        else
                        {
                            GPU_LogError("Using per-vertex attributes.\n");
                            color_attr.format.is_per_sprite = 0;
                            color_attr.values = colors;
                        }
                    }
                }
            }
            
            mouse_state = SDL_GetMouseState(&mx, &my);
            if(mouse_state & (SDL_BUTTON_LMASK | SDL_BUTTON_RMASK))
            {
                if(mx <= 150 && my <= 150)
                {
                    if(mouse_state & SDL_BUTTON_LMASK)
                    {
                        float dx = mx/3 - src_rect.x;
                        float dy = my/3 - src_rect.y;
                        src_rect.x = mx/3;
                        src_rect.y = my/3;
                        src_rect.w -= dx;
                        src_rect.h -= dy;
                    }
                    else if(mouse_state & SDL_BUTTON_RMASK)
                    {
                        src_rect.w = mx/3 - src_rect.x;
                        src_rect.h = my/3 - src_rect.y;
                    }
                }
            }
            
            GPU_SetUniformf(timeloc, SDL_GetTicks()/1000.0f);
            
            GPU_Clear(screen);
            
            if(use_color_expansion)
                GPU_SetAttributeSource(numSprites, color_attr);
            else
                GPU_SetAttributeSource(4*numSprites, color_attr);
            
            for(i = 0; i < numSprites; i++)
            {
                GPU_Rect r = {src_rects[4*i], src_rects[4*i+1], src_rects[4*i+2], src_rects[4*i+3]};
                GPU_Blit(image, &r, screen, positions[2*i], positions[2*i+1]);
            }
            //GPU_BlitBatchSeparate(image, screen, numSprites, positions, src_rects, expanded_colors, 0);
            
            set_shader(0, NULL);
            
            GPU_BlitScale(image, NULL, screen, 75, 75, 3.0f, 3.0f);
            GPU_Rectangle(screen, 3*src_rect.x, 3*src_rect.y, 3*(src_rect.x + src_rect.w), 3*(src_rect.y + src_rect.h), red);
            GPU_CircleFilled(screen, 3*src_rect.x, 3*src_rect.y, 4, blue);
            GPU_CircleFilled(screen, 3*(src_rect.x + src_rect.w), 3*(src_rect.y + src_rect.h), 4, green);
            
            if(shader_index == 1)
                set_shader(p, &block);
            
            
            GPU_Flip(screen);
            
            frameCount++;
            if(frameCount%500 == 0)
                printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
        }
        
        printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
        
        GPU_FreeImage(image);
        
        free_shaders(v, f, p);
	}
	
	GPU_Quit();
	
	return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: shadowmint/sdl-gpu
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;
}
コード例 #3
0
ファイル: main.c プロジェクト: DataFighter/sdl-gpu
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;
}