Пример #1
0
Bool renderText(GPU_Target* renderTarget, GC gc, int x, int y, const char* string) {
    LOG("Rendering text: '%s'\n", string);
    if (string == NULL || string[0] == '\0') { return True; }
    GraphicContext* gContext = GET_GC(gc);
    SDL_Color color = {
            GET_RED_FROM_COLOR(gContext->foreground),
            GET_GREEN_FROM_COLOR(gContext->foreground),
            GET_BLUE_FROM_COLOR(gContext->foreground),
            GET_ALPHA_FROM_COLOR(gContext->foreground),
    };
    SDL_Surface* fontSurface = TTF_RenderUTF8_Blended(GET_FONT(gContext->font), string, color);
    if (fontSurface == NULL) {
        return False;
    }
    GPU_Image* fontImage = GPU_CopyImageFromSurface(fontSurface);
    SDL_FreeSurface(fontSurface);
    if (fontImage == NULL) {
        return False;
    }
    y -= TTF_FontAscent(GET_FONT(gContext->font));
    GPU_Blit(fontImage, NULL, renderTarget, x + fontImage->w / 2, y + fontImage->h / 2);
    GPU_FreeImage(fontImage);
    GPU_Flip(renderTarget);
    return True;
}
Пример #2
0
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 = GPU_LoadImage("data/test.bmp");
        if(image == NULL)
            return -1;
        
        
        startTime = SDL_GetTicks();
        frameCount = 0;
        
        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;
                }
            }
            
            GPU_Clear(screen);
            
            GPU_BlitTransform(image, NULL, screen, screen->w/2, screen->h/2, 360*sin(SDL_GetTicks()/2000.0f), 2.5*sin(SDL_GetTicks()/1000.0f), 2.5*sin(SDL_GetTicks()/1200.0f));
            
            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);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #3
0
void destroy_group(Group* groups, int i)
{
    GPU_FreeImage(groups[i].sprite.image);
    SDL_DestroyWindow(SDL_GetWindowFromID(groups[i].target->context->windowID));
    GPU_FreeTarget(groups[i].target);
    groups[i].target = NULL;
}
Пример #4
0
void FileSystem::clear()
{
    // Delete the stored images
    typedef SCML_PAIR(int,int) pair_type;
    SCML_BEGIN_MAP_FOREACH_CONST(images, pair_type, GPU_Image*, item)
    {
        GPU_FreeImage(item);
    }
Пример #5
0
int main(int argc, char* argv[])
{
	GPU_Target* screen;
	
	// Prepare renderer for SDL_gpu to use
	GPU_RendererID rendererID = GPU_MakeRendererID("Dummy", GPU_ReserveNextRendererEnum(), 1, 0);
	GPU_RegisterRenderer(rendererID, &create_dummy_renderer, &free_dummy_renderer);

	printRenderers();
	
	// Request this specific renderer
	screen = GPU_InitRenderer(rendererID.renderer, 800, 600, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
		return -1;
	
	printCurrentRenderer();
	
	{
		Uint8 done;
		SDL_Event event;
		
		GPU_Image* image = GPU_LoadImage("data/test.bmp");
		if(image == NULL)
            GPU_Log("Failed to load image.\n");
        
        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;
                }
            }
            
            GPU_Clear(screen);
        
            GPU_Blit(image, NULL, screen, screen->w/2, screen->h/2);
            
            GPU_Flip(screen);
            
            // Long delay to keep the logging from piling up too much
            SDL_Delay(500);
        }
        
        GPU_FreeImage(image);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #6
0
bool FileSystem::loadImageFile(int folderID, int fileID, const std::string& filename)
{
    // Load an image and store it somewhere accessible by its folder/file ID combo.
    GPU_Image* img = GPU_LoadImage(SCML_TO_CSTRING(filename));
    if(img == NULL)
        return false;
    if(!SCML_MAP_INSERT(images, SCML_MAKE_PAIR(folderID, fileID), img))
    {
        printf("SCML_SDL_gpu::FileSystem failed to load image: Loading %s duplicates a folder/file id (%d/%d)\n", SCML_TO_CSTRING(filename), folderID, fileID);
        GPU_FreeImage(img);
        return false;
    }
    return true;
}
Пример #7
0
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();

	{
		GPU_Image* image;
		float x, y;
		float velx, vely;
		float dt;
		Uint32 startTime;
		long frameCount;
		Uint8 done;
		SDL_Event event;

		image = GPU_LoadImage("data/test3.png");
		if (image == NULL)
			return -1;

		x = 0;
		y = 0;
		velx = 50.0f;
		vely = 70.0f;

		dt = 0.010f;

		startTime = SDL_GetTicks();
		frameCount = 0;

		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;
				}
			}

			x += velx*dt;
			y += vely*dt;
			if (x < 0)
			{
				x = 0;
				velx = -velx;
			}
			else if (x + image->w > screen->w)
			{
				x = screen->w - image->w;
				velx = -velx;
			}

			if (y < 0)
			{
				y = 0;
				vely = -vely;
			}
			else if (y + image->h > screen->h)
			{
				y = screen->h - image->h;
				vely = -vely;
			}

			GPU_Clear(screen);

			GPU_Blit(image, NULL, screen, 50, 50);
			GPU_Blit(image, NULL, screen, 150, 50);
			GPU_Blit(image, NULL, screen, 350, 250);

			GPU_SetBlending(image, 1);
			GPU_SetRGBA(image, 255, 100, 255, 127.5f + 127.5f*sin(SDL_GetTicks() / 1000.0f));

			GPU_Blit(image, NULL, screen, x, y);

			GPU_SetRGBA(image, 255, 255, 255, 255);
			GPU_SetBlending(image, 0);

			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);
	}

	GPU_Quit();
	
	return 0;
}
Пример #8
0
int main(int argc, char* argv[])
{
	GPU_Target* screen;

	printRenderers();
	GPU_SetPreInitFlags(GPU_INIT_DISABLE_VSYNC);
	screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
		return -1;
	
	printCurrentRenderer();
	
	{
		Uint32 startTime;
		long frameCount;
		Uint8 done;
		SDL_Event event;
		
        float dt = 0.010f;
        
        int maxSprites = 50000;
        int numSprites = 101;
        
        float* x = (float*)malloc(sizeof(float)*maxSprites);
        float* y = (float*)malloc(sizeof(float)*maxSprites);
        float* velx = (float*)malloc(sizeof(float)*maxSprites);
        float* vely = (float*)malloc(sizeof(float)*maxSprites);
        int i;
        
        GPU_Image* image = GPU_LoadImage("data/small_test.png");
        if(image == NULL)
            return -1;
        
        GPU_SetSnapMode(image, GPU_SNAP_NONE);
        
        startTime = SDL_GetTicks();
        frameCount = 0;
        for(i = 0; i < maxSprites; i++)
        {
            x[i] = rand()%screen->w;
            y[i] = rand()%screen->h;
            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];
        }
        
        
        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_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();
                    }
                }
            }
            
            for(i = 0; i < numSprites; i++)
            {
                x[i] += velx[i]*dt;
                y[i] += vely[i]*dt;
                if(x[i] < 0)
                {
                    x[i] = 0;
                    velx[i] = -velx[i];
                }
                else if(x[i]> screen->w)
                {
                    x[i] = screen->w;
                    velx[i] = -velx[i];
                }
                
                if(y[i] < 0)
                {
                    y[i] = 0;
                    vely[i] = -vely[i];
                }
                else if(y[i]> screen->h)
                {
                    y[i] = screen->h;
                    vely[i] = -vely[i];
                }
            }
            
            GPU_Clear(screen);
            
            for(i = 0; i < numSprites; i++)
            {
                GPU_Blit(image, NULL, screen, x[i], y[i]);
            }
            
            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(x);
        free(y);
        free(velx);
        free(vely);
        
        GPU_FreeImage(image);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #9
0
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;
}
Пример #10
0
int main(int argc, char* argv[])
{
	printRenderers();
	
	GPU_Target* screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
		return -1;
	
	printCurrentRenderer();
	
	int numImages = 0;
	
	GPU_Image* images[argc-1];
	int i;
	for(i = 1; i < argc; i++)
	{
		images[numImages] = GPU_LoadImage(argv[i]);
		if(images[numImages] != NULL)
			numImages++;
	}
	
	Uint8* keystates = SDL_GetKeyState(NULL);
	GPU_Camera camera = GPU_GetDefaultCamera();
	
	float dt = 0.010f;
	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;
			}
		}
		
		if(keystates[KEY_UP])
		{
			camera.y -= 200*dt;
		}
		else if(keystates[KEY_DOWN])
		{
			camera.y += 200*dt;
		}
		if(keystates[KEY_LEFT])
		{
			camera.x -= 200*dt;
		}
		else if(keystates[KEY_RIGHT])
		{
			camera.x += 200*dt;
		}
		if(keystates[KEY_MINUS])
		{
			camera.zoom -= 1.0f*dt;
		}
		else if(keystates[KEY_EQUALS])
		{
			camera.zoom += 1.0f*dt;
		}
		
		GPU_ClearRGBA(screen, 255, 255, 255, 255);
		
		GPU_SetCamera(screen, &camera);
		
		float x = 100;
		float y = 100;
		for(i = 0; i < numImages; i++)
		{
			float x_scale = 150.0f/images[i]->w;
			float y_scale = 150.0f/images[i]->h;
			GPU_BlitScale(images[i], NULL, screen, x, y, x_scale, y_scale);
			
			x += 200;
			
			if((i+1)%4 == 0)
			{
				x = 100;
				y += 200;
			}
		}
		
		
		GPU_Flip(screen);
		SDL_Delay(10);
	}
	
	for(i = 0; i < numImages; i++)
	{
		GPU_FreeImage(images[i]);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #11
0
int do_interleaved(GPU_Target* screen)
{
	GPU_LogError("do_interleaved()\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;
	
	int floats_per_sprite = 2 + 4 + 4;
	float* sprite_values = (float*)malloc(sizeof(float)*maxSprites*floats_per_sprite);
	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++)
	{
		sprite_values[val_n++] = rand()%screen->w;
		sprite_values[val_n++] = rand()%screen->h;
		sprite_values[val_n++] = 0;
		sprite_values[val_n++] = 0;
		sprite_values[val_n++] = image->w;
		sprite_values[val_n++] = image->h;
		sprite_values[val_n++] = rand()%256;
		sprite_values[val_n++] = rand()%256;
		sprite_values[val_n++] = rand()%256;
		sprite_values[val_n++] = rand()%256;
		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 = 2;
                }
				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();
				}
			}
		}
		
		for(i = 0; i < numSprites; i++)
		{
		    val_n = floats_per_sprite*i;
			sprite_values[val_n] += velx[i]*dt;
			sprite_values[val_n+1] += vely[i]*dt;
			if(sprite_values[val_n] < 0)
			{
				sprite_values[val_n] = 0;
				velx[i] = -velx[i];
			}
			else if(sprite_values[val_n] > screen->w)
			{
				sprite_values[val_n] = screen->w;
				velx[i] = -velx[i];
			}
			
			if(sprite_values[val_n+1] < 0)
			{
				sprite_values[val_n+1] = 0;
				vely[i] = -vely[i];
			}
			else if(sprite_values[val_n+1] > screen->h)
			{
				sprite_values[val_n+1] = screen->h;
				vely[i] = -vely[i];
			}
		}
		
		GPU_Clear(screen);
		
        GPU_BlitBatch(image, screen, numSprites, sprite_values, 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);
	
	return return_value;
}
Пример #12
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;
}
Пример #13
0
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;
        GPU_Image* image1;
        GPU_Image* image1a;
        SDL_Color yellow = {246, 255, 0};
        SDL_Color red = {200, 0, 0};
        GPU_Image* image2;
        GPU_Image* image3;
        GPU_Image* image4;
        
        image = GPU_LoadImage("data/test3.png");
        if(image == NULL)
            return -1;
        
        image1 = GPU_CopyImage(image);
        
        makeColorTransparent(image1, yellow);
        
        image1a = GPU_CopyImage(image);
        
        replaceColor(image1a, yellow, red);
        
        
        
        
        image2 = GPU_CopyImage(image);
        
        shiftHSV(image2, 100, 0, 0);
        
        image3 = GPU_CopyImage(image);
        
        shiftHSV(image3, 0, -100, 0);
        
        image4 = GPU_CopyImage(image);
        
        shiftHSV(image4, 0, 0, 100);
        
        startTime = SDL_GetTicks();
        frameCount = 0;
        
        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;
                }
            }
            
            GPU_Clear(screen);
            
            GPU_Blit(image, NULL, screen, 150, 150);
            GPU_Blit(image1, NULL, screen, 300, 150);
            GPU_Blit(image1a, NULL, screen, 450, 150);
            GPU_Blit(image2, NULL, screen, 150, 300);
            GPU_Blit(image3, NULL, screen, 300, 300);
            GPU_Blit(image4, NULL, screen, 450, 300);
            
            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);
        GPU_FreeImage(image1);
        GPU_FreeImage(image1a);
        GPU_FreeImage(image2);
        GPU_FreeImage(image3);
        GPU_FreeImage(image4);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #14
0
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();

	{
		int numImages;
		GPU_Image** images;
		int i;
		const Uint8* keystates;
		GPU_Camera camera;
		float dt;
		Uint8 done;
		SDL_Event event;
		float x, y;

		images = (GPU_Image**)malloc(sizeof(GPU_Image*)*(argc - 1));

		numImages = 0;

		for (i = 1; i < argc; i++)
		{
			images[numImages] = GPU_LoadImage(argv[i]);
			if (images[numImages] != NULL)
				numImages++;
		}

		keystates = SDL_GetKeyState(NULL);
		camera = GPU_GetDefaultCamera();

		dt = 0.010f;
		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_r)
					{
						camera.x = 0.0f;
						camera.y = 0.0f;
						camera.z = -10.0f;
						camera.zoom = 1.0f;
						camera.angle = 0.0f;
					}
				}
				else if (event.type == SDL_MOUSEBUTTONDOWN)
				{
					float x, y;
					GPU_GetVirtualCoords(screen, &x, &y, event.button.x, event.button.y);

					printScreenToWorld(x, y);
				}
			}

			if (keystates[KEY_UP])
			{
				camera.y -= 200 * dt;
			}
			else if (keystates[KEY_DOWN])
			{
				camera.y += 200 * dt;
			}
			if (keystates[KEY_LEFT])
			{
				camera.x -= 200 * dt;
			}
			else if (keystates[KEY_RIGHT])
			{
				camera.x += 200 * dt;
			}
			if (keystates[KEY_MINUS])
			{
				camera.zoom -= 1.0f*dt;
			}
			else if (keystates[KEY_EQUALS])
			{
				camera.zoom += 1.0f*dt;
			}

			GPU_ClearRGBA(screen, 255, 255, 255, 255);

			GPU_SetCamera(screen, &camera);

			x = 0;
			y = 0;
			for (i = 0; i < numImages; i++)
			{
				x += images[i]->w / 2.0f;
				y += images[i]->h / 2.0f;
				GPU_Blit(images[i], NULL, screen, x, y);
				x += images[i]->w / 2.0f;
				y += images[i]->h / 2.0f;
			}


			GPU_Flip(screen);
			SDL_Delay(10);
		}

		for (i = 0; i < numImages; i++)
		{
			GPU_FreeImage(images[i]);
		}

		free(images);
	}

	GPU_Quit();

	return 0;
}
Пример #15
0
int main(int argc, char* argv[])
{
	GPU_Target* screen;

	GPU_SetDebugLevel(GPU_DEBUG_LEVEL_MAX);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

	GPU_SetRequiredFeatures(GPU_FEATURE_BASIC_SHADERS);
	screen = GPU_InitRenderer(GPU_RENDERER_OPENGL_3, 800, 600, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
    {
        GPU_LogError("Initialization Error: Could not create a renderer with proper feature support for this demo.\n");
		return 1;
    }
    
    glewExperimental = GL_TRUE;  // Force GLEW to get exported functions instead of checking via extension string
    if(glewInit() != GLEW_OK)
    {
        GPU_LogError("Initialization Error: Failed to initialize GLEW.\n");
        return 2;
    }

	{
		GPU_Image* image;
		float dt;
		Uint32 startTime;
		long frameCount;
		int maxSprites = 50;
		int numSprites;
		float x[50];
		float y[50];
		float velx[50];
		float vely[50];
		int i;
		Uint8 done;
		SDL_Event event;

		image = GPU_LoadImage("data/test.bmp");
		if (image == NULL)
			return 3;
        
        v = GPU_LoadShader(GPU_VERTEX_SHADER, "data/shaders/untextured-150.vert");
        f = GPU_LoadShader(GPU_FRAGMENT_SHADER, "data/shaders/untextured-150.frag");
        p = GPU_LinkShaders(v, f);
            
        GPU_Log("%s\n", GPU_GetShaderMessage());

        glUseProgram(p);
        vertex_loc = GPU_GetAttributeLocation(p, "gpu_Vertex");
        color_loc = GPU_GetAttributeLocation(p, "gpu_Color");
        modelViewProjection_loc = GPU_GetUniformLocation(p, "gpu_ModelViewProjectionMatrix");

        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);

        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);

		dt = 0.010f;

		startTime = SDL_GetTicks();
		frameCount = 0;

		numSprites = 1;

		for (i = 0; i < maxSprites; i++)
		{
			x[i] = rand() % screen->w;
			y[i] = rand() % screen->h;
			velx[i] = 10 + rand() % screen->w / 10;
			vely[i] = 10 + rand() % screen->h / 10;
		}


		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_EQUALS || event.key.keysym.sym == SDLK_PLUS)
					{
						if (numSprites < maxSprites)
							numSprites++;
					}
					else if (event.key.keysym.sym == SDLK_MINUS)
					{
						if (numSprites > 0)
							numSprites--;
					}
				}
			}

			for (i = 0; i < numSprites; i++)
			{
				x[i] += velx[i] * dt;
				y[i] += vely[i] * dt;
				if (x[i] < 0)
				{
					x[i] = 0;
					velx[i] = -velx[i];
				}
				else if (x[i]> screen->w)
				{
					x[i] = screen->w;
					velx[i] = -velx[i];
				}

				if (y[i] < 0)
				{
					y[i] = 0;
					vely[i] = -vely[i];
				}
				else if (y[i]> screen->h)
				{
					y[i] = screen->h;
					vely[i] = -vely[i];
				}
			}

			GPU_Clear(screen);

			draw_3d_stuff(screen);

			for (i = 0; i < numSprites; i++)
			{
				GPU_Blit(image, NULL, screen, x[i], y[i]);
			}

			draw_more_3d_stuff(screen);

			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);
	}

	GPU_Quit();
	
	return 0;
}
Пример #16
0
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;
		int mx, my;
		GPU_Image* image;
		GPU_Target* target;
		SDL_Color c;
		
        image = GPU_LoadImage("data/test.bmp");
        if(image == NULL)
            return -1;
        
        target = GPU_LoadTarget(image);
        
        startTime = SDL_GetTicks();
        frameCount = 0;
        
        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;
                }
            }
            
            SDL_GetMouseState(&mx, &my);
            c = GPU_GetPixel(target, mx - 50, my - 50);
            
            GPU_ClearRGBA(screen, c.r, c.g, c.b, GET_ALPHA(c));
            
            GPU_Blit(image, NULL, screen, image->w/2 + 50, image->h/2 + 50);
            
            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);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #17
0
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;
        Uint32 v, f, p;
        GPU_ShaderBlock block;
        int uloc;
        int timeloc;
        float dt;
#define MAX_SPRITES 50
        int numSprites;
        float x[MAX_SPRITES];
        float y[MAX_SPRITES];
        float velx[MAX_SPRITES];
        float vely[MAX_SPRITES];
        int i;

        image = GPU_LoadImage("data/test.bmp");
        if(image == NULL)
            return -1;

        block = load_shaders(&v, &f, &p);
        uloc = GPU_GetUniformLocation(p, "tex");
        GPU_SetUniformi(uloc, 0);
        timeloc = GPU_GetUniformLocation(p, "time");

        dt = 0.010f;

        startTime = SDL_GetTicks();
        frameCount = 0;

        numSprites = 1;

        for(i = 0; i < MAX_SPRITES; i++)
        {
            x[i] = rand()%screen->w;
            y[i] = rand()%screen->h;
            velx[i] = 10 + rand()%screen->w/10;
            vely[i] = 10 + rand()%screen->h/10;
        }


        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_EQUALS || event.key.keysym.sym == SDLK_PLUS)
                    {
                        if(numSprites < MAX_SPRITES)
                            numSprites++;
                    }
                    else if(event.key.keysym.sym == SDLK_MINUS)
                    {
                        if(numSprites > 0)
                            numSprites--;
                    }
                    else if(event.key.keysym.sym == SDLK_SPACE)
                    {
                        if(GPU_IsDefaultShaderProgram(GPU_GetCurrentShaderProgram()))
                        {
                            GPU_ActivateShaderProgram(p, &block);
                            uloc = GPU_GetUniformLocation(p, "tex");
                            GPU_SetUniformi(uloc, 0);
                            timeloc = GPU_GetUniformLocation(p, "time");
                        }
                        else
                            GPU_ActivateShaderProgram(0, NULL);
                    }
                }
            }

            for(i = 0; i < numSprites; i++)
            {
                x[i] += velx[i]*dt;
                y[i] += vely[i]*dt;
                if(x[i] < 0)
                {
                    x[i] = 0;
                    velx[i] = -velx[i];
                }
                else if(x[i]> screen->w)
                {
                    x[i] = screen->w;
                    velx[i] = -velx[i];
                }

                if(y[i] < 0)
                {
                    y[i] = 0;
                    vely[i] = -vely[i];
                }
                else if(y[i]> screen->h)
                {
                    y[i] = screen->h;
                    vely[i] = -vely[i];
                }
            }

            GPU_SetUniformf(timeloc, SDL_GetTicks()/1000.0f);

            GPU_Clear(screen);

            for(i = 0; i < numSprites; i++)
            {
                GPU_Blit(image, NULL, screen, x[i], y[i]);
            }

            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;
}
Пример #18
0
int main(int argc, char* argv[])
{
	printRenderers();
	
	GPU_Target* screen = GPU_Init(1024, 700, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
		return -1;
	
	printCurrentRenderer();
	
	GPU_Image* image = GPU_LoadImage("data/small_test.bmp");
	if(image == NULL)
		return -1;
    
    
	SDL_Surface* font_surface = GPU_LoadSurface("data/comic14.png");
	DemoFont* font = FONT_Alloc(font_surface);
	GPU_SetRGB(font->image, 255, 0, 0);
	SDL_FreeSurface(font_surface);
	
    
    GPU_Rect rect1 = {0.0f, 0.0f, image->w*3, image->h*3};
    GPU_Rect rect2 = {-image->w*2, -image->h*2, image->w*3, image->h*3};
	
	Uint32 startTime = SDL_GetTicks();
	long frameCount = 0;
	
	
	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;
			}
		}
		
		GPU_Clear(screen);
		
		
		float x = 0;
		float y = 0;
		FONT_Draw(font, screen, x + 10, y + 10, "NONE");
		x += 40;
		y += 20;
		GPU_SetWrapMode(image, GPU_WRAP_NONE, GPU_WRAP_NONE);
		GPU_Blit(image, &rect1, screen, x + rect1.w/2, y + rect1.h/2);
		x += image->w*4;
		GPU_Blit(image, &rect2, screen, x + rect2.w/2, y + rect2.h/2);
		
		x = 0;
		y += (rect1.h + 10);
		FONT_Draw(font, screen, x + 10, y + 10, "REPEAT");
		x += 40;
		y += 20;
		GPU_SetWrapMode(image, GPU_WRAP_REPEAT, GPU_WRAP_REPEAT);
		GPU_Blit(image, &rect1, screen, x + rect1.w/2, y + rect1.h/2);
		x += image->w*4;
		GPU_Blit(image, &rect2, screen, x + rect2.w/2, y + rect2.h/2);
		
		x = 0;
		y += (rect1.h + 10);
		FONT_Draw(font, screen, x + 10, y + 10, "MIRRORED");
		x += 40;
		y += 20;
		GPU_SetWrapMode(image, GPU_WRAP_MIRRORED, GPU_WRAP_MIRRORED);
		GPU_Blit(image, &rect1, screen, x + rect1.w/2, y + rect1.h/2);
		x += image->w*4;
		GPU_Blit(image, &rect2, screen, x + rect2.w/2, y + rect2.h/2);
		
		x = 500;
		y = 0;
		FONT_Draw(font, screen, x + 10, y + 10, "REPEAT/MIRRORED");
		x += 40;
		y += 20;
		GPU_SetWrapMode(image, GPU_WRAP_REPEAT, GPU_WRAP_MIRRORED);
		GPU_Blit(image, &rect1, screen, x + rect1.w/2, y + rect1.h/2);
		x += image->w*4;
		GPU_Blit(image, &rect2, screen, x + rect2.w/2, y + rect2.h/2);
		
		x = 500;
		y += (rect1.h + 10);
		FONT_Draw(font, screen, x + 10, y + 10, "NONE/REPEAT");
		x += 40;
		y += 20;
		GPU_SetWrapMode(image, GPU_WRAP_NONE, GPU_WRAP_REPEAT);
		GPU_Blit(image, &rect1, screen, x + rect1.w/2, y + rect1.h/2);
		x += image->w*4;
		GPU_Blit(image, &rect2, screen, x + rect2.w/2, y + rect2.h/2);
		
		x = 500;
		y += (rect1.h + 10);
		FONT_Draw(font, screen, x + 10, y + 10, "NONE/MIRRORED");
		x += 40;
		y += 20;
		GPU_SetWrapMode(image, GPU_WRAP_NONE, GPU_WRAP_MIRRORED);
		GPU_Blit(image, &rect1, screen, x + rect1.w/2, y + rect1.h/2);
		x += image->w*4;
		GPU_Blit(image, &rect2, screen, x + rect2.w/2, y + rect2.h/2);
		
		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));
	
	FONT_Free(font);
	GPU_FreeImage(image);
	GPU_Quit();
	
	return 0;
}
Пример #19
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;
}
Пример #20
0
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();

	{
		GPU_Image* image;
		GPU_Target* alias_target;
		GPU_Image* alias_image;
		Uint32 startTime;
		long frameCount;

		Uint8 done;
		SDL_Event event;

		image = GPU_LoadImage("data/test.bmp");
		if (image == NULL)
			return -1;

		alias_target = GPU_CreateAliasTarget(screen);

		GPU_SetViewport(screen, GPU_MakeRect(50, 30, 400, 300));

		GPU_SetViewport(alias_target, GPU_MakeRect(400, 30, 400, 300));
		GPU_SetTargetRGBA(alias_target, 255, 100, 100, 200);


		alias_image = GPU_CreateAliasImage(image);

		GPU_SetImageFilter(alias_image, GPU_FILTER_NEAREST);
		GPU_SetRGBA(alias_image, 100, 255, 100, 200);


		startTime = SDL_GetTicks();
		frameCount = 0;

		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;
				}
			}

			GPU_Clear(screen);

			GPU_Blit(image, NULL, screen, image->w / 2, image->h / 2);
			GPU_Blit(alias_image, NULL, screen, image->w + alias_image->w / 2, alias_image->h / 2);

			GPU_Blit(image, NULL, alias_target, image->w / 2, image->h / 2);
			GPU_Blit(alias_image, NULL, alias_target, image->w + alias_image->w / 2, alias_image->h / 2);

			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(alias_image);
		GPU_FreeImage(image);
		GPU_FreeTarget(alias_target);
	}

	GPU_Quit();
	
	return 0;
}
Пример #21
0
int do_interleaved(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;

    int floats_per_vertex = 2 + 2 + 4;
    float* vertex_values = (float*)malloc(sizeof(float)*max_vertices*floats_per_vertex);
    float* velx = (float*)malloc(sizeof(float)*max_vertices/3);
    float* vely = (float*)malloc(sizeof(float)*max_vertices/3);
    int i;

    GPU_Image* image = GPU_LoadImage("data/test3.png");
    GPU_LogError("do_interleaved()\n");
    if(image == NULL)
        return -1;

    startTime = SDL_GetTicks();
    frameCount = 0;

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


    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 = 2;
                }
                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();
                }
            }
        }

        // 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_Clear(screen);

        GPU_TriangleBatch(image, screen, num_vertices, vertex_values, 0, NULL, GPU_BATCH_XY_ST_RGBA);

        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);

    return return_value;
}
Пример #22
0
int main(int argc, char* argv[])
{
	GPU_Target* screen;

	printRenderers();
	
	screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);
	if(screen == NULL)
    {
        GPU_LogError("Failed to init SDL_gpu.\n");
		return -1;
    }
	
	printCurrentRenderer();
	
	{
		Uint32 startTime;
		long frameCount;
		Uint8 done;
		SDL_Event event;
		
		GPU_Image* image;
		GPU_Image* image1;
        
        GPU_LogError("Loading image\n");
        image = GPU_LoadImage(IMAGE_FILE);
        if(image == NULL)
        {
            GPU_LogError("Failed to load image.\n");
            return -1;
        }
        
        GPU_LogError("Saving image\n");
        GPU_SaveImage(image, SAVE_FILE, GPU_FILE_AUTO);
        
        GPU_LogError("Reloading image\n");
        image1 = GPU_LoadImage(SAVE_FILE);
        if(image1 == NULL)
        {
            GPU_LogError("Failed to reload image.\n");
            return -1;
        }
        
        
        startTime = SDL_GetTicks();
        frameCount = 0;
        
        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;
                }
            }
            
            GPU_Clear(screen);
            
            GPU_Blit(image, NULL, screen, screen->w/4, screen->h/2);
            GPU_Blit(image1, NULL, screen, 3*screen->w/4, screen->h/2);
            
            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);
        GPU_FreeImage(image1);
	}
	
	GPU_Quit();
	
	return 0;
}
Пример #23
0
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;
		const Uint8* keystates;
        GPU_Camera camera;
		float dt;
		SDL_Surface* surface;
		GPU_Image* image;
		GPU_Image* image1;
		GPU_Image* image2;
		GPU_Image* image3;
		GPU_Image* image4;
		
        image = GPU_LoadImage("data/test.bmp");
        //image = GPU_LoadImage("data/big_test.png");
        if(image == NULL)
            return -1;

        // Copying the annoying way
        image1 = GPU_CreateImage(image->w, image->h, GPU_FORMAT_RGBA);
        GPU_LoadTarget(image1);
        GPU_Blit(image, NULL, image1->target, image1->target->w/2, image1->target->h/2);
        GPU_FreeTarget(image1->target);

        // Copying the normal way
        image2 = GPU_CopyImage(image);

        // Copying from a surface dump
        surface = GPU_CopySurfaceFromImage(image);
        //GPU_SaveSurface(surface, "save_surf1.bmp", GPU_FILE_AUTO);
        image3 = GPU_CopyImageFromSurface(surface);
        SDL_FreeSurface(surface);

        // A buffer for window capture
        image4 = NULL;


        keystates = SDL_GetKeyState(NULL);
        camera = GPU_GetDefaultCamera();

        startTime = SDL_GetTicks();
        frameCount = 0;

        dt = 0.010f;
        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)
                    {
                        // Take a window capture
                        GPU_FreeImage(image4);
                        image4 = GPU_CopyImageFromTarget(screen);
                    }
                }
            }
            
            if(keystates[KEY_UP])
            {
                camera.y -= 200*dt;
            }
            else if(keystates[KEY_DOWN])
            {
                camera.y += 200*dt;
            }
            if(keystates[KEY_LEFT])
            {
                camera.x -= 200*dt;
            }
            else if(keystates[KEY_RIGHT])
            {
                camera.x += 200*dt;
            }
            if(keystates[KEY_MINUS])
            {
                camera.zoom -= 1.0f*dt;
            }
            else if(keystates[KEY_EQUALS])
            {
                camera.zoom += 1.0f*dt;
            }
            
            
            GPU_ClearRGBA(screen, 100, 100, 100, 255);
            
            GPU_SetCamera(screen, &camera);
            
            GPU_Blit(image, NULL, screen, 128, 128);
            GPU_Blit(image1, NULL, screen, 128 + 256, 128);
            GPU_Blit(image2, NULL, screen, 128 + 512, 128);
            GPU_Blit(image3, NULL, screen, 128, 128 + 256);
            
            if(image4 != NULL)
                GPU_BlitScale(image4, NULL, screen, 3*screen->w/4, 3*screen->h/4, 0.25f, 0.25f);
            
            GPU_Flip(screen);
            
            frameCount++;
            if(frameCount%500 == 0)
                GPU_LogError("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
        }

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

        GPU_FreeImage(image);
        GPU_FreeImage(image1);
        GPU_FreeImage(image2);
        GPU_FreeImage(image3);
        GPU_FreeImage(image4);
	}
	
	GPU_Quit();
	
	return 0;
}