I've been trying to integrate agar with an existing SDL/opengl program
of mine.  I wasn't getting too far, so backed off that for the moment,
and tried to just get OpenGL working with agar.

I was trying to follow: 
http://wiki.libagar.org/wiki/Integrating_Agar_in_an_existing_GL_application
but that seems to be quite a bit out of date (compared with svn trunk).

I replaced most of the MyEventLoop() code with the MyEventLoop() code from the
customeventloop in the demos.

I got it to compile, but it doesn't display any opengl stuff, and the one 
window that
seems to be displayed doesn't respond to the mouse...

(I've attached my version for reference.)

Is there a better starting point?  My goal is to use agar for widgets on top
of my SDL/opengl program.  I'd prefer to continue to use the SDL interface,
so perhaps I'd be better off using agar 1.3 (just a guess) instead?


#include <stdio.h>
#include <string.h>
#include <math.h>

#include <agar/core.h>
#include <agar/gui.h>
#include <agar/gui/opengl.h>
#include <SDL.h>
 
GLfloat spin = 0.0f;
GLdouble vz = -5.0;
GLfloat ambient[4] = { 0.5f, 1.0f, 1.0f, 1.0f };
GLfloat diffuse[4] = { 0.5f, 1.0f, 1.0f, 1.0f };
GLfloat specular[4] = { 0.5f, 1.0f, 1.0f, 1.0f };
 
static GLdouble isoVtx[12][3] = {    
#define X .525731112119133606 
#define Z .850650808352039932
    {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},    
    {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},    
    {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} 
};
static GLuint isoInd[20][3] = { 
    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},    
    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},    
    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, 
    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
 
 
float mProjection[16];        /* Projection matrix to load for 'background' */
float mModelview[16];        /* Modelview matrix to load for 'background' */
float mTexture[16];        /* Texture matrix to load for 'background' */
 
 
int showUI = 1;                /* Show Agar GUI */
int bgFocused = 0;        /* Background focus */
int wireframe = 0;        /* Wireframe mode */

const int nominalFPS = 1000/30;
int curFPS = 0;
int pressedKey = 0;
int xClick = 0;
int yClick = 0;
 
#if 1
static void
Norm(GLdouble *a)
{
    GLdouble d = sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
    a[0] /= d;
    a[1] /= d;
    a[2] /= d;
}
 
static void
DrawTriangle(GLdouble *a, GLdouble *b, GLdouble *c, int div, float r)
{
    if (div <= 0) {
        glNormal3dv(a); glVertex3d(a[0]*r, a[1]*r, a[2]*r);
        glNormal3dv(b); glVertex3d(b[0]*r, b[1]*r, b[2]*r);
        glNormal3dv(c); glVertex3d(c[0]*r, c[1]*r, c[2]*r);
    } else {
        GLdouble ab[3], ac[3], bc[3];
        int i;
 
        for (i = 0; i < 3; i++) {
            ab[i] = (a[i]+b[i])/2.0;
            ac[i] = (a[i]+c[i])/2.0;
            bc[i] = (b[i]+c[i])/2.0;
        }
        Norm(ab);
        Norm(ac);
        Norm(bc);
        DrawTriangle(a, ab, ac, div-1, r);
        DrawTriangle(b, bc, ab, div-1, r);
        DrawTriangle(c, ac, bc, div-1, r);
        DrawTriangle(ab, bc, ac, div-1, r);
    }
}
 
/* Render the test object. */
static void
MyDrawFunction()
{
    int i;
    GLfloat pos[4];
 
    glLoadIdentity();
    glPushAttrib(GL_POLYGON_BIT|GL_LIGHTING_BIT|GL_DEPTH_BUFFER_BIT);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_POLYGON);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
    glShadeModel(GL_SMOOTH);
    pos[0] = 10.0f;
    pos[1] = 10.0f;
    pos[2] = 0.0f;
    pos[3] = 1.0f;
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 10.0f);
 
    glPushMatrix();
    glTranslated(0.0, 0.0, vz);
    glRotatef(spin,0.0f,1.0f,0.0f);
    glRotatef(spin,1.0f,1.0f,1.0f);
    glBegin(GL_TRIANGLES);
    for (i = 0; i < 20; i++) {
        DrawTriangle(isoVtx[isoInd[i][0]],
                     isoVtx[isoInd[i][1]],
                     isoVtx[isoInd[i][2]],
                     2, 1.0f);
    }
    glEnd();
 
    glPopMatrix();
    glPopAttrib();
 
    if (++spin > 360.0f) { spin -= 360.0f; }
}
#endif
 
static void
BG_Display(void)
{
    MyDrawFunction();
}
 
static void
BG_Resize(SDL_Event *ev)
{
    GLdouble xMin, xMax, yMin, yMax;
 
    glMatrixMode(GL_TEXTURE); glPushMatrix(); glLoadIdentity();
    glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();
 
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
 
    /* Set a 60 degrees field of view with 1.0 aspect ratio. */
    yMax = 0.01*tan(0.523598f);
    yMin = -yMax;
    xMin = yMin;
    xMax = yMax;
    glFrustum(xMin, xMax, yMin, yMax, 0.01, 100.0);
 
    glGetFloatv(GL_PROJECTION_MATRIX, mProjection);
    glGetFloatv(GL_MODELVIEW_MATRIX, mModelview);
    glGetFloatv(GL_TEXTURE_MATRIX, mTexture);
 
    glMatrixMode(GL_PROJECTION); glPopMatrix();
    glMatrixMode(GL_MODELVIEW); glPopMatrix();
    glMatrixMode(GL_TEXTURE); glPopMatrix();
}
 
/*
 * Push GL states and call BG_Display().
 */
static void 
BG_Draw()
{
    glMatrixMode(GL_TEXTURE); glPushMatrix(); glLoadMatrixf(mTexture);
    glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadMatrixf(mProjection);
    glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadMatrixf(mModelview);
 
    /* Save the GL state (XXX we don't need to save all) */
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glEnable(GL_CULL_FACE);
    glDisable(GL_CLIP_PLANE0);
    glDisable(GL_CLIP_PLANE1);
    glDisable(GL_CLIP_PLANE2);
    glDisable(GL_CLIP_PLANE3);
 
    /* Now render background */
    BG_Display();
 
    /* Restore the previous GL states */
    glPopAttrib();
    glMatrixMode(GL_MODELVIEW); glPopMatrix();
    glMatrixMode(GL_TEXTURE); glPopMatrix();
    glMatrixMode(GL_PROJECTION); glPopMatrix();
}
 
/*
 * Here you BG gain focus
 * and you can do some autohide menu for example, etc.
 */ 
void
BG_GainFocus()
{
    bgFocused = 1;
    vz += 1.0;    /* Move shape closer */
}
 
void
BG_LostFocus()
{
    bgFocused = 0;
    vz -= 1.0;     /* Move shape back */ 
}
 
/*
 * Trap for mouse events. If we return 0, the event is
 * passed on to Agar.
 */
static int
TrapMouseEvent(SDL_Event *ev)
{
    int rv = 0;
    int x,y;
 
    switch (ev->type) {
    case SDL_MOUSEBUTTONDOWN:
        x = ev->button.x;
        y = ev->button.y;
 
        switch (ev->button.button) {
        case SDL_BUTTON_WHEELUP:
            vz -= 0.1;
            break;
        case SDL_BUTTON_WHEELDOWN:
            vz += 0.1;
            break;
        }
        return 1;
    }
    return (rv);
}
 
/* Our custom keypress callback routine. */
static int
MyKeyCallback(SDL_Event *ev)
{
        /* ... */
        return (0);
}
 
/*
 * Our custom event loop
 */
static void
MyEventLoop(void)
{
	AG_Driver *drv;
	AG_Window *win;
	int t1, t2;
	AG_DriverEvent dev;

	t1 = AG_GetTicks();
	for (;;) {
		t2 = AG_GetTicks();

		if (t2-t1 >= nominalFPS) {
			/*
			 * Case 1: Update the video display.
			 */
			AG_LockVFS(&agDrivers);

			/* Render the Agar windows */
			if (agDriverSw) {
				/* With single-window drivers (e.g., sdlfb). */
				AG_BeginRendering(agDriverSw);

				BG_Draw();

				if (showUI) {
					AG_FOREACH_WINDOW(win, agDriverSw) {
						AG_ObjectLock(win);
						AG_WindowDraw(win);
						AG_ObjectUnlock(win);
					}
				}

				AG_EndRendering(agDriverSw);
			} else {
			   fprintf(stderr, "Multiple native windows isn't supported\n");
			}

			AG_UnlockVFS(&agDrivers);

			t1 = AG_GetTicks();
			curFPS = nominalFPS - (t1-t2);
			if (curFPS < 1) { curFPS = 1; }

		} else if (AG_PendingEvents(NULL) > 0) {
			/*
			 * Case 2: There are events waiting to be processed.
			 */
			do {
				/* Retrieve the next queued event. */
				if (AG_GetNextEvent(NULL, &dev) == 1) {
					switch (dev.type) {
					case AG_DRIVER_MOUSE_BUTTON_DOWN:
						xClick = dev.data.button.x;
						yClick = dev.data.button.y;
						printf("Click at %d,%d!\n",
						    dev.data.button.x,
						    dev.data.button.y);
						break;
					case AG_DRIVER_KEY_DOWN:
						pressedKey = (int)dev.data.key.ks;
						printf("Key down: %d (0x%x)\n",
						    (int)dev.data.key.ks,
						    (int)dev.data.key.ucs);

						// This shouldn't be necessary...
						if (dev.data.key.ks == 27) {
						   return;
						}
						break;
					default:
						break;
					}

					/* Forward the event to Agar. */
					if (AG_ProcessEvent(NULL, &dev) == -1)
						return;
				}
			} while (AG_PendingEvents(NULL) > 0);

		} else if (AG_TIMEOUTS_QUEUED()) {
			/*
			 * Case 3: There are AG_Timeout(3) callbacks to run.
			 */
			AG_ProcessTimeouts(t2);
		} else {
			/*
			 * Case 4: Nothing to do, idle.
			 */
			AG_Delay(1);
		}
	}
}

 
int
main(int argc, char *argv[])
{
    AG_Window *win;
    AG_GLView *glv;
    AG_Box *hb;
    AG_HSVPal *pal;
    int i;
 
    AG_InitCore("agar-background-demo", 0);
 
    /* Pass AG_VIDEO_OPENGL flag to require an OpenGL display. */
    AG_InitVideo(640, 480, 32, AG_VIDEO_OPENGL|AG_VIDEO_RESIZABLE);
 
    /* Create a settings window. */
    win = AG_WindowNew(0);
    hb = AG_BoxNewHoriz(win, 0);
    AG_Expand(hb);
    {
            AG_Notebook *nb;
            AG_NotebookTab *ntab;
 
            nb = AG_NotebookNew(hb, AG_NOTEBOOK_VFILL);
        AG_Expand(nb);
        {
                    ntab = AG_NotebookAddTab(nb, "Amb", AG_BOX_VERT);
                    pal = AG_HSVPalNew(ntab, AG_HSVPAL_VFILL);
                    AG_BindFloat(pal, "RGBAv", ambient);
 
                    ntab = AG_NotebookAddTab(nb, "Dif", AG_BOX_VERT);
                    pal = AG_HSVPalNew(ntab, AG_HSVPAL_VFILL);
                    AG_BindFloat(pal, "RGBAv", diffuse);
 
                    ntab = AG_NotebookAddTab(nb, "Spe", AG_BOX_VERT);
                    pal = AG_HSVPalNew(ntab, AG_HSVPAL_VFILL);
                    AG_BindFloat(pal, "RGBAv", specular);
        }
 
        AG_ButtonNewInt(hb, AG_BUTTON_STICKY,
            "Wireframe Mode", &wireframe);
    }
    AG_WindowShow(win);
 
    /* Create a bunch of test windows */
/*
*/
    for (i = 0; i < 5; i++) {
        win = AG_WindowNew(0);
        AG_WindowSetCaption(win, "TL%d", i);
        AG_LabelNew(win, 0, "Top Left %d", i);
        AG_WindowSetPosition(win, AG_WINDOW_TL, 1);
        AG_WindowShow(win);
    }
 
    /* Enter our custom event loop */
    MyEventLoop();
    AG_Destroy();
    return (0);
}
_______________________________________________
Agar mailing list
[email protected]
http://libagar.org/lists.html

Reply via email to