#include <v_lib.h>

#define size 512

int main()
{
	u32_t fc, starttime;
	u8_t *fb;
    input_record_t ir;
    if(vglCreateWindow("AGP bus speed test",10,10,size,size)==NULL)
    	return -1;
    fb=(u8_t*)memAlloc(size * size * 4);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,1,0,1,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

	/* Load texture */
	glTexImage2D(GL_TEXTURE_2D,0,4,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,fb);
		
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
   	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

	/*
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
	*/

    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
   	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
   	
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
   	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

	glEnable(GL_TEXTURE_2D);
        
	starttime=timeTickCount();
    for(fc=0;;fc++)
    {
    	while(vglPending())
        {
        	vglNextEvent(&ir);
            if(ir.first_code == key_escape)
            	goto cleanup;
		}
        memFill32(fb,(u8_t)(fc & 255),size * size);
        
        glClear(GL_COLOR_BUFFER_BIT);

		/* Reload texture */
		glTexSubImage2D(GL_TEXTURE_2D,0,0,0,size,size,GL_RGBA,GL_UNSIGNED_BYTE,fb);

        glBegin(GL_QUADS);
	        glColor3f(1,1,1);
        	glTexCoord2f(0,0);
        	glVertex2f(0,0);

	        glColor3f(0,1,0);
        	glTexCoord2f(0,1);
        	glVertex2f(0,1);

	        glColor3f(1,1,1);
        	glTexCoord2f(1,1);
        	glVertex2f(1,1);

	        glColor3f(0,1,0);
        	glTexCoord2f(1,0);
        	glVertex2f(1,0);
		glEnd();
        vglSwapBuffers();
	}
cleanup:;
	
	starttime=timeTickCount() - starttime;
	
	vglDestroyWindow(vglGetCurrentWindow());
    memFree(fb);
    
    printf("%d frames, %g seconds, %g FPS\n",fc,0.001f * starttime,
    	1000.0f * fc / starttime);

    return 0;
}
