Aros/Developer/OpenGL

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Navbar for the Aros wikibook
Aros User
Aros User Docs
Aros User FAQs
Aros User Applications
Aros User DOS Shell
Aros/User/AmigaLegacy
Aros Dev Docs
Aros Developer Docs
Porting Software from AmigaOS/SDL
For Zune Beginners
Zune .MUI Classes
For SDL Beginners
Aros Developer BuildSystem
Specific platforms
Aros x86 Complete System HCL
Aros x86 Audio/Video Support
Aros x86 Network Support
Aros Intel AMD x86 Installing
Aros Storage Support IDE SATA etc
Aros Poseidon USB Support
x86-64 Support
Motorola 68k Amiga Support
Linux and FreeBSD Support
Windows Mingw and MacOSX Support
Android Support
Arm Raspberry Pi Support
PPC Power Architecture
misc
Aros Public License

Introduction[edit | edit source]

The Amiga's first introduction to hardware enhanced 3D was Warp3D. This was followed by an open source(software based) equivalent called Wazp3D A Wazp3D adaptation to AROS from Matthias Rustler appeared on December 2007.


During 2009 Krzysztof "Deadwood" Smiechowicz ported version 7.5 of MESA 3D to AROS. MESA 3D provides a generic OpenGL implementation. Then he added hardware 3D acceleration capabilities to AROS with his port of Gallium3D for MESA 3D. Then on September 2011 Wazp3D was linked to Mesa 3D for hardware enhanced 3D.

in -> vertex -> fragment -> out

  • Framebuffer – alpha, stencil, depth
  • Vertex – transform and lighting
  • Fragment – texturing, fog,
  • Geometry – primitives (dots, lines, polygons triangle/quad), clipping
  • Compute -
  • Tessellation -


Far better using SDL's 1.2.x GL additions for up to and including OpenGL 2 or consider SDL 1.3 / 2.x for OpenGL 3 and above

  • Web GL 1.0 – OpenGL ES 2.0 – OpenGL 2 but complete support in OpenGL 4.1
  • Web GL 2.0 – Open GL ES 3.0 – OpenGL 4.3

There is no backwards compatibility from OpenGL 2 to OpenGL 1 as well as OpenGL ES 2 to OpenGL ES 1. But all later OpenGL and ES versions are retrospectively support.


Considered deprecated (ie not advised using or missing/removed)

GL1.1 – OpenGL ES 1 with Fixed Functions – glNormalPointer, glColorPointer and glVertexPointer were introduced. Nearly all of the old ID software are OpenGL 1.1 + extensions

GL1.3 -

GL1.4 – fragment support added

GL1.5 – VBOs added – OpenGL ES 1.1 – added features such as mandatory support for multitexture, better multitexture support (including combiners and dot product texture operations), automatic mipmap generation, vertex buffer objects, state queries, user clip planes, and greater control over point rendering

GL2.0 – OpenGL es 2 – Vertex / Fragment shaders introduced – WebGL minimum – GLSL shading language -

deprecated = glMatrixMode glMult/LoadMatrix glRotate/Translate/Scale glPush/PopMatrix glBegin/End glVertex glTexCoords glLight glMaterial glPush/PopAttrib

GL2.1 – 2006 – last appearance of some matrix functions/stacks – PBOs Pixel buffer objects -

If you want to be able to run on hardware incapable of OpenGL 3.0, then suggest that you not write applications for OpenGL 3.0. Many of the features of 3.0 that can be used on older hardware are backported via extensions. So instead of writing a 3.x application, you could be writing a 2.1 application that is able to use certain extensions. So just code to the GL 2.1 spec instead. You can always research the calls and features that have been since deprecated and just avoid using them in a pure 2.1 program

general method is to have different rendering paths for different features. You detect the version number. If it's one thing, you use one rendering path. If it's lower, you use another. Etc. You can do fine-grained checking by checking for the presence of certain extensions.

Minimum advised starting point for new apps which arrived with Mesa 12 ported to AROS which opened access to the following (hardware support permitting)

Gl3.0 – 2008 – Geometry shaders – Vertex Array Objects (VAOs) ie GL_ARB_vertex_array_object. Older versions need to do more vertex setup work when drawing an object, rather than setting them up once and just binding the VAO. Modern OpenGL enforces you to write your own matrices and pass those manually into shader programs. use programmable function pipelines

GL3.1 – eliminates most of the fixed-function rendering pipeline in favor of a programmable one

GL3.3

GL4.0 – 2010 – Tesselation shaders – Image load/store and atomic counters are probably the biggest ones (this includes shader storage buffers too), though simultaneously the least advertised. Shaders get to write/read from images and buffers directly, though there are a lot of caveats and synchronization issues to understand about doing this. introduces an additional pair of shader stages: one that decides how much to tessellate a primitive, and the other to decide how to generate the new values from the tessellated primitive.

GL4.1 – 2010 – incompatibilities between the desktop version of OpenGL and OpenGL ES 2.0 persisted until OpenGL 4.1, which added the GL_ARB_ES2_compatibility extension. Subroutines are an interesting but often overlooked feature. Basically, you can attach subroutines to shader programs like uniforms. So you could use the same basic program that will run a lighting function, but you can swap lighting functions without changing the program. The basic program would fetch textures, figure out what the diffuse/specular/etc material parameters are, determine the normal and lighting parameters, and pass that to the subroutine. allows you to dynamically piece together different fragments of shaders. You can more or less attach a function to a specific bind point in a program.

GL4.2 – 2011 -

GL4.3 – 2012 – Compute shaders – openGL ES 3 compatibility -

GL4.4

GL4.5


Compiling[edit | edit source]

WARP3D[edit | edit source]

Recompiling a program that use Warp3D.library is quite simple. You just need to link it to Warp3D

So compiling cow3d for Aros x86 was as simple as gcc -c -O3 CoW3D-3.c -o Cow3D-Aros -lWarp3D

Wazp3D-Prefs is a little tool for selecting how Wazp3D works (fast or nice).

Aros version is included with Aminet/Cow3D (see Wazp3D homepage for documentation) Option "Renderer:Soft to Image" is safer if your Aros system dont support LockBitmapTags() else "Renderer:Soft to Bitmap" If your Aros support "Native Graphics Aros" then you can try "Renderer:hard overlay" or "Renderer:hard" but the display is not perfect as Mesa dont support rendering to bitmap as Warp3D

You need to use W3D_DrawTriFan(), W3D_DrawTriSttrip() or better still, W3D_DrawArray() with their corresponding data structures. Suggest the latter since you have much more control over how the vertex data is organised.

You create a triangle strip or a triangle fan. In a strip, alternate triangles share two points along an edge. In a fan, all triangles share one common vertex and adjacent triangles also share an edge. The simplest to illustrate is the strip. A rectangle is simply a 2 triangle strip, something like this:

V[0]--V[1]
|      /|
|     / |
|    /  |
|   /   |
|  /    |
V[2]--V[3]

Using one of the strip drawing methods, the same texture is applied to all polygons in the strip. If you use the old single triangle routines, you have to either enable global texture environment or specify the texture in each polygon separately.

Warp3D is a rasterizer only. That means that it draws primitives in screen space. Strictly, the axes are defined as X=left to right in pixels, Y top to botton in pixels, Z is plane of the screen into the distance. The valid range for Z is 0.0 – 1.0.

You need to calculate your vertices in screen space directly or write your own transformation pipeline.

See Starship3D

MESA 3D[edit | edit source]

MESA 12[edit | edit source]

Creating a window in SDL and binding an OpenGL 3.2 context to it uses these steps

Initialize the SDL video subsystem using SDL_Init or SDL_VideoInit
Set the parameters we require for opengl using SDL_GL_SetAttribute
Create a window using SDL_CreateWindow
Bind an OpenGL context to the window using SDL_GL_CreateContext

MESA 7[edit | edit source]

From client perspective using mesa.library is quite simple:

1) For programs that simply use OpenGL and Glut (like Aminet/starship) then just compile them

gcc starship.c -lglut -lgl – o starshiparos

2) For programs that use OpenGL and Amiga windowing system

  • Create a window
  • Call AROSMesaCreateContext passing it the window and some other stuff -> you get rendering context in return
  • Call AROSMesaMakeCurrent passing it the context so that AROSMesa knows what to render on
  • Render some stuff using glXXX functions
  • Call AROSMesaSwapBuffers to have the content of render buffer painted onto your window
  • Loop to glXXX functions

[...]

  • Call AROSMesaDestroyContext(context);
  • Call CloseWindow(window);

OpenGL API has been changed in ABI V1, just rename 'glA' to 'AROSMesa' The VBO functions are still there, but you need to access them via glaGetProcAddress.

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <cybergraphx/cybergraphics.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>

#include <proto/timer.h>
#include <devices/timer.h>
#include <proto/cybergraphics.h>

#define GL_GLEXT_PROTOTYPES
#include <GL/arosmesa.h>

#include <stdio.h>

AROSMesaContext     glcont=NULL;
double              angle = 0.0;
double              angle_inc = 0.0;
BOOL                finished = FALSE;
struct Window *     win = NULL;
struct Device *     TimerBase = NULL;
struct timerequest  timereq;
struct MsgPort      timeport;
struct Library *    CyberGfxBase = NULL;
BOOL                fullscreen = FALSE;

GLuint              fragmentShader = 0;
GLuint              vertexShader = 0;
GLuint              shaderProgram = 0;
GLint               angleLocation = 0;

const GLchar * fragmentShaderSource =
"uniform float angle;"
"void main()"
"{"
"   vec4 v = vec4(gl_Color);"
"   float intensity = abs(1.0f - (mod(angle, 1440.0f) / 720.0f));"
"   v.b = v.b * intensity;"
"   v.g = v.g * (1.0f - intensity);"
"	gl_FragColor = v;"
"}";

const GLchar * vertexShaderSource =
"void main()"
"{	"
"   gl_FrontColor = gl_Color;"
"	gl_Position = ftransform();"
"}";

#define RAND_COL 1.0f
#define DEGREES_PER_SECOND 180.0
#define USE_PERSPECTIVE 1

void prepare_shader_program()
{
#define BUFFER_LEN 2048
    char buffer[BUFFER_LEN] = {0};
    int len;

    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    glGetShaderInfoLog(fragmentShader, BUFFER_LEN, &len, buffer);
    printf("Fragment shader compile output: %s\n", buffer);

    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    glGetShaderInfoLog(vertexShader, BUFFER_LEN, &len, buffer);
    printf("Vertex shader compile output: %s\n", buffer);

    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glGetProgramInfoLog(shaderProgram, BUFFER_LEN, &len, buffer);
    printf("Shader program compile output: %s\n", buffer);

#undef BUFFER_LEN
}

void cleanup_shader_program()
{
    glUseProgram(0);
    glDetachShader(shaderProgram, fragmentShader);
    glDetachShader(shaderProgram, vertexShader);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteProgram(shaderProgram);
}

void render_face()
{
    glBegin(GL_QUADS);
        glColor4f(RAND_COL , 0.0, RAND_COL, 0.3);
        glVertex3f(-0.25, -0.25, 0.0);
        glColor4f(0, RAND_COL, RAND_COL, 0.3);
        glVertex3f(-0.25, 0.25, 0.0);
        glColor4f(0 , 0, 0, 0.3);
        glVertex3f(0.25, 0.25, 0.0);
        glColor4f(RAND_COL , RAND_COL, 0, 0.3);
        glVertex3f(0.25, -0.25, 0.0);
    glEnd();

}

void render_cube()
{
    glPushMatrix();
    glRotatef(0.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    render_face();
    glPopMatrix();

    glPushMatrix();
    glRotatef(90.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    render_face();
    glPopMatrix();

    glPushMatrix();
    glRotatef(180.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    render_face();
    glPopMatrix();

    glPushMatrix();
    glRotatef(270.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    render_face();
    glPopMatrix();

    glPushMatrix();
    glRotatef(90.0, 1.0, 0.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    render_face();
    glPopMatrix();

    glPushMatrix();
    glRotatef(-90.0, 1.0, 0.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    render_face();
    glPopMatrix();
}

void render_triangle()
{
    glBegin(GL_TRIANGLES);
        glColor4f(1.0, 0.0, 0.0, 1.0);
        glVertex3f(-0.25, -0.25, 0.0);
        glColor4f(0.0, 1.0, 0.0, 1.0);
        glVertex3f(-0.25,  0.25, 0.0);
        glColor4f(0.0, 0.0, 1.0, 1.0);
        glVertex3f( 0.25,  0.25, 0.0);
    glEnd();
}

void render()
{
    glLoadIdentity();
    glClearColor(0.3, 0.3, 0.3, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glCullFace(GL_BACK);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);

    angle += angle_inc;
    glUniform1f(angleLocation, angle);

#if USE_PERSPECTIVE == 1
    glTranslatef(0.0, 0.0, -6.0);
#endif
    glPushMatrix();
    glRotatef(angle, 0.0, 1.0, 0.0);
    glTranslatef(0.0, 0.0, 0.25);
    glRotatef(angle, 1.0, 0.0, 1.0);
    render_cube();
    glPopMatrix();

    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);

    AROSMesaSwapBuffers(glcont);
}

#define VISIBLE_WIDTH 300
#define VISIBLE_HEIGHT 300

void initmesa()
{
    struct TagItem attributes [ 14 ]; /* 14 should be more than enough :) */
    int i = 0;
    GLfloat h = 0.0f;

    attributes[i].ti_Tag = AMA_Window;      attributes[i++].ti_Data = (IPTR)win;
    attributes[i].ti_Tag = AMA_Left;        attributes[i++].ti_Data = win->BorderLeft;
    attributes[i].ti_Tag = AMA_Top;         attributes[i++].ti_Data = win->BorderTop;
    attributes[i].ti_Tag = AMA_Bottom;      attributes[i++].ti_Data = win->BorderBottom;
    attributes[i].ti_Tag = AMA_Right;       attributes[i++].ti_Data = win->BorderRight;

    // double buffer ?
    attributes[i].ti_Tag = AMA_DoubleBuf;   attributes[i++].ti_Data = GL_TRUE;

    // RGB(A) Mode ?
    attributes[i].ti_Tag = AMA_RGBMode;     attributes[i++].ti_Data = GL_TRUE;

    /* Stencil/Accum */
    attributes[i].ti_Tag = AMA_NoStencil;   attributes[i++].ti_Data = GL_TRUE;
    attributes[i].ti_Tag = AMA_NoAccum;     attributes[i++].ti_Data = GL_TRUE;

    // done...
    attributes[i].ti_Tag    = TAG_DONE;

    glcont = AROSMesaCreateContext(attributes);
    if (glcont)
    {
        AROSMesaMakeCurrent(glcont);
        h = (GLfloat)VISIBLE_HEIGHT / (GLfloat)VISIBLE_WIDTH ;

        glViewport(0, 0, (GLint) VISIBLE_WIDTH, (GLint) VISIBLE_HEIGHT);
#if USE_PERSPECTIVE == 1
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustum(-1.0, 1.0, -h, h, 5.0, 200.0);
        glMatrixMode(GL_MODELVIEW);
#endif
        prepare_shader_program();
        glUseProgram(shaderProgram);
        angleLocation = glGetUniformLocation(shaderProgram, "angle");
    }
    else
        finished = TRUE; /* Failure. Stop */
}

void deinitmesa()
{
    if (glcont)
    {
        cleanup_shader_program();
        AROSMesaDestroyContext(glcont);
    }
}

static int init_timerbase()
{
    timeport.mp_Node.ln_Type   = NT_MSGPORT;
    timeport.mp_Node.ln_Pri    = 0;
    timeport.mp_Node.ln_Name   = NULL;
    timeport.mp_Flags          = PA_IGNORE;
    timeport.mp_SigTask        = FindTask(NULL);
    timeport.mp_SigBit         = 0;
    NEWLIST(&timeport.mp_MsgList);

    timereq.tr_node.io_Message.mn_Node.ln_Type    = NT_MESSAGE;
    timereq.tr_node.io_Message.mn_Node.ln_Pri     = 0;
    timereq.tr_node.io_Message.mn_Node.ln_Name    = NULL;
    timereq.tr_node.io_Message.mn_ReplyPort       = &timeport;
    timereq.tr_node.io_Message.mn_Length          = sizeof (timereq);

    if(OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest *)&timereq,0) == 0)
    {
        TimerBase = (struct Device *)timereq.tr_node.io_Device;
        return 1;
    }
    else
    {
        return 0;
    }
}

static void deinit_timerbase()
{
    if (TimerBase != NULL)
        CloseDevice((struct IORequest *)&timereq);
}

void HandleIntuiMessages(void)
{
    struct IntuiMessage *msg;

    while((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
    {
        switch(msg->Class)
        {
        case IDCMP_CLOSEWINDOW:
            finished = TRUE;
            break;
        case IDCMP_VANILLAKEY:
            if (msg->Code == 27 /* ESC */) finished = TRUE;
            break;
        }
        ReplyMsg((struct Message *)msg);
    }
}

#define ARG_FULLSCREEN  0
#define NUM_ARGS        1

STATIC CONST_STRPTR   TEMPLATE=(CONST_STRPTR) "FULLSCREEN/S";
static struct RDArgs  *myargs;
static IPTR           args[NUM_ARGS];

void get_arguments(void)
{
    if((myargs = ReadArgs(TEMPLATE, args, NULL)))
    {
        fullscreen = (BOOL)args[ARG_FULLSCREEN];
        FreeArgs(myargs);
    }
}

/*
** Open a simple window using OpenWindowTagList()
*/
int main(void)
{
    ULONG fps = 0;
//    ULONG exitcounter = 0;
    TEXT title[100];
    struct Screen * pubscreen = NULL;
    struct Screen * customscreen = NULL;
    LONG modeid;

    struct timeval tv;
    UQUAD lastmicrosecs = 0L;
    UQUAD currmicrosecs = 0L;
    UQUAD fpsmicrosecs = 0L;

    get_arguments();

    init_timerbase();

    GetSysTime(&tv);
    lastmicrosecs = tv.tv_secs * 1000000 + tv.tv_micro;
    fpsmicrosecs = lastmicrosecs;

    if (fullscreen)
    {
        CyberGfxBase = OpenLibrary("cybergraphics.library", 0L);

        modeid = BestCModeIDTags(CYBRBIDTG_NominalWidth, VISIBLE_WIDTH,
                                    CYBRBIDTG_NominalHeight, VISIBLE_HEIGHT,
                                    TAG_DONE);

        customscreen = OpenScreenTags(NULL,
                            SA_Type,        CUSTOMSCREEN,
                            SA_DisplayID,   modeid,
                            SA_Width,       VISIBLE_WIDTH,
                            SA_Height,      VISIBLE_HEIGHT,
                            SA_ShowTitle,   FALSE,
                            SA_Quiet,       TRUE,
                            TAG_DONE);

        win = OpenWindowTags(NULL,
                WA_Left,            0,
                WA_Top,             200,
                WA_InnerWidth,      VISIBLE_WIDTH,
                WA_InnerHeight,     VISIBLE_HEIGHT,
                WA_CustomScreen,    (IPTR)customscreen,
                WA_Flags,           WFLG_ACTIVATE | WFLG_BACKDROP | WFLG_BORDERLESS | WFLG_RMBTRAP,
                WA_IDCMP,           IDCMP_VANILLAKEY,
                TAG_DONE);
    }
    else
    {
        if ((pubscreen = LockPubScreen(NULL)) == NULL) return 1;

        win = OpenWindowTags(0,
                            WA_Title, (IPTR)"MesaSimpleRendering",
                            WA_PubScreen, pubscreen,
                            WA_CloseGadget, TRUE,
                            WA_DragBar, TRUE,
                            WA_DepthGadget, TRUE,
                            WA_Left, 50,
                            WA_Top, 200,
                            WA_InnerWidth, VISIBLE_WIDTH,
                            WA_InnerHeight, VISIBLE_HEIGHT,
                            WA_Activate, TRUE,
                            WA_RMBTrap, TRUE,
                            WA_SimpleRefresh, TRUE,
                            WA_NoCareRefresh, TRUE,
                            WA_IDCMP, IDCMP_VANILLAKEY | IDCMP_CLOSEWINDOW,
                            TAG_DONE);

        UnlockPubScreen(NULL, pubscreen);
    }

    initmesa();
//    finished = TRUE;
    while(!finished)
    {
        GetSysTime(&tv);
        currmicrosecs = tv.tv_secs * 1000000 + tv.tv_micro;

        if (currmicrosecs - fpsmicrosecs > 1000000)
        {
            /* FPS counting is naive! */
            fpsmicrosecs += 1000000;
            sprintf(title, "MesaSimpleRendering, FPS: %d", fps);
            SetWindowTitles(win, title, (UBYTE *)~0L);
            fps = 0;
        }

        angle_inc = ((double)(currmicrosecs - lastmicrosecs) / 1000000.0) * DEGREES_PER_SECOND;
        lastmicrosecs = currmicrosecs;

        fps++;
        render();
        HandleIntuiMessages();
//        exitcounter++;
//        Delay(10);
//        if (exitcounter > 0) finished = TRUE;
    }

    deinitmesa();

    deinit_timerbase();

    CloseWindow(win);

    if (customscreen) CloseScreen(customscreen);

    if (CyberGfxBase) CloseLibrary(CyberGfxBase);

    return 0;
}
/*
Copyright (C) 2006-2011 Mark Olsen

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/extensions.h>
#include <cybergraphx/cybergraphics.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/cybergraphics.h>

#include <GL/arosmesa.h>

#include "quakedef.h"
#include "input.h"
#include "keys.h"
#include "gl_local.h"
#include "in_morphos.h"
#include "vid_mode_morphos.h"

struct Library *MesaBase = 0;

struct display
{
	void *inputdata;

	unsigned int width, height;
	int fullscreen;
	char used_mode[256];

	struct Screen *screen;
	struct Window *window;

	void *pointermem;

	char pal[256*4];

	AROSMesaContext mesacontext;
};

void Sys_Video_CvarInit(void)
{
}

void *Sys_Video_Open(const char *mode, unsigned int width, unsigned int height, int fullscreen, unsigned char *palette)
{
	struct display *d;
	struct modeinfo modeinfo;
	char monitorname[128];
	int r;
	int i;

	d = AllocVec(sizeof(*d), MEMF_CLEAR);
	if (d)
	{
		MesaBase = OpenLibrary("mesa.library", 0);
		if (MesaBase)
		{
			if (fullscreen)
			{
				if (*mode && modeline_to_modeinfo(mode, &modeinfo))
				{
					snprintf(monitorname, sizeof(monitorname), "%s.monitor", modeinfo.monitorname);
					d->screen = OpenScreenTags(0,
						SA_Width, modeinfo.width,
						SA_Height, modeinfo.height,
						SA_Depth, modeinfo.depth,
#if 0
						SA_MonitorName, monitorname,
#endif
						SA_Quiet, TRUE,
						TAG_DONE);
				}
				else
				{
					d->screen = OpenScreenTags(0,
						SA_Quiet, TRUE,
						TAG_DONE);
				}

				if (d->screen)
				{
					width = d->screen->Width;
					height = d->screen->Height;

					snprintf(d->used_mode, sizeof(d->used_mode), "Dunno,%d,%d,42", width, height);
				}
				else
					fullscreen = 0;
			}

			if (d->screen || !fullscreen)
			{
				d->window = OpenWindowTags(0,
					d->screen?WA_Width:WA_InnerWidth, width,
					d->screen?WA_Height:WA_InnerHeight, height,
					WA_Title, "FodQuake",
					WA_DragBar, d->screen?FALSE:TRUE,
					WA_DepthGadget, d->screen?FALSE:TRUE,
					WA_Borderless, d->screen?TRUE:FALSE,
					WA_RMBTrap, TRUE,
					d->screen?WA_CustomScreen:TAG_IGNORE, (IPTR)d->screen,
					WA_Activate, TRUE,
					TAG_DONE);

				if (d->window)
				{
					d->mesacontext = AROSMesaCreateContextTags(
						AMA_Window, d->window,
						AMA_Left, d->screen?0:d->window->BorderLeft,
						AMA_Top, d->screen?0:d->window->BorderTop,
						AMA_Width, width,
						AMA_Height, height,
						AMA_NoStencil, TRUE,
						AMA_NoAccum, TRUE,
						TAG_DONE);

					if (d->mesacontext)
					{
						AROSMesaMakeCurrent(d->mesacontext);

						d->pointermem = AllocVec(256, MEMF_ANY|MEMF_CLEAR);
						if (d->pointermem)
						{
							SetPointer(d->window, d->pointermem, 16, 16, 0, 0);

							d->width = width;
							d->height = height;
							d->fullscreen = fullscreen;

							d->inputdata = Sys_Input_Init(d->screen, d->window);
							if (d->inputdata)
							{
								return d;
							}

							FreeVec(d->pointermem);
						}

						AROSMesaMakeCurrent(0);
						AROSMesaDestroyContext(d->mesacontext);
					}

					CloseWindow(d->window);
				}

				if (d->screen)
					CloseScreen(d->screen);
			}

			CloseLibrary(MesaBase);
		}

		FreeVec(d);
	}

	return 0;
}

void Sys_Video_Close(void *display)
{
	struct display *d = display;

	Sys_Input_Shutdown(d->inputdata);

	AROSMesaMakeCurrent(0);
	AROSMesaDestroyContext(d->mesacontext);

	CloseWindow(d->window);

	FreeVec(d->pointermem);

	if (d->screen)
		CloseScreen(d->screen);

	CloseLibrary(MesaBase);

	FreeVec(d);
}

unsigned int Sys_Video_GetNumBuffers(void *display)
{
	struct display *d;

	d = display;

	return d->screen ? 3 : 1;
}

int Sys_Video_GetKeyEvent(void *display, keynum_t *keynum, qboolean *down)
{
	struct display *d = display;

	return Sys_Input_GetKeyEvent(d->inputdata, keynum, down);
}

void Sys_Video_GetMouseMovement(void *display, int *mousex, int *mousey)
{
	struct display *d = display;

	Sys_Input_GetMouseMovement(d->inputdata, mousex, mousey);
}

void Sys_Video_SetWindowTitle(void *display, const char *text)
{
	struct display *d;

	d = display;

	SetWindowTitles(d->window, text, (void *)-1);
}

unsigned int Sys_Video_GetWidth(void *display)
{
	struct display *d;

	d = display;

	return d->width;
}

unsigned int Sys_Video_GetHeight(void *display)
{
	struct display *d;

	d = display;

	return d->height;
}

qboolean Sys_Video_GetFullscreen(void *display)
{
	struct display *d;

	d = display;

	return d->fullscreen;
}

const char *Sys_Video_GetMode(void *display)
{
	struct display *d;

	d = display;

	return d->used_mode;
}

void Sys_Video_BeginFrame(void *display, unsigned int *x, unsigned int *y, unsigned int *width, unsigned int *height)
{
	struct display *d;

	d = display;

	*x = 0;
	*y = 0;
	*width = d->width;
	*height = d->height;
}

void Sys_Video_Update(void *display, vrect_t *rects)
{
	struct display *d = display;

	AROSMesaSwapBuffers(d->mesacontext);
}

void Sys_Video_GrabMouse(void *display, int dograb)
{
	struct display *d = display;

	if (!d->screen)
	{
		Sys_Input_GrabMouse(d->inputdata, dograb);

		if (dograb)
		{
			/* Hide pointer */

			SetPointer(d->window, d->pointermem, 16, 16, 0, 0);
		}
		else
		{
			/* Show pointer */

			ClearPointer(d->window);
		}
	}
}

void Sys_Video_SetGamma(void *display, unsigned short *ramps)
{
}

qboolean Sys_Video_HWGammaSupported(void *display)
{
	return 0;
}

int Sys_Video_FocusChanged(void *display)
{
	return 0;
}


GLSL openGL Shader Language[edit | edit source]

Shaders are written in the C-like language GLSL. GLSL is tailored for use with graphics and contains useful features specifically targeted at vector and matrix manipulation.

Shaders always begin with a version declaration, followed by a list of input and output variables, uniforms and its main function. Each shader's entry point is at its main function where we process any input variables and output the results in its output variables.

#version version_number
in type in_variable_name;
in type in_variable_name;
out type out_variable_name;
  
uniform type uniform_name;
  
void main()
{
  // process input(s) and do some weird graphics stuff
  ...
  // output processed stuff to output variable
  out_variable_name = weird_stuff_we_processed;
}

When we're talking specifically about the vertex shader each input variable is also known as a vertex attribute. There is a maximum number of vertex attributes we're allowed to declare limited by the hardware. OpenGL guarantees there are always at least 16 4-component vertex attributes available, but some hardware may allow for more which you can retrieve by querying GL_MAX_VERTEX_ATTRIBS:

int nrAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl;

This often returns the minimum of 16 which should be more than enough for most purposes.


Types

GLSL has, like any other programming language, data types for specifying what kind of variable we want to work with. GLSL has most of the default basic types we know from languages like C: int, float, double, uint and bool. GLSL also features two container types that we'll be using a lot, namely vectors and matrices. We'll discuss matrices in a later chapter.


Vectors

A vector in GLSL is a 2,3 or 4 component container for any of the basic types just mentioned. They can take the following form (n represents the number of components):

  • vecn: the default vector of n floats.
  • bvecn: a vector of n booleans.
  • ivecn: a vector of n integers.
  • uvecn: a vector of n unsigned integers.
  • dvecn: a vector of n double components.

Most of the time we will be using the basic vecn since floats are sufficient for most of our purposes.

Components of a vector can be accessed via vec.x where x is the first component of the vector. You can use .x, .y, .z and .w to access their first, second, third and fourth component respectively. GLSL also allows you to use rgba for colors or stpq for texture coordinates, accessing the same components.

The vector datatype allows for some interesting and flexible component selection called swizzling. Swizzling allows us to use syntax like this:

vec2 someVec;
vec4 differentVec = someVec.xyxx;
vec3 anotherVec = differentVec.zyw;
vec4 otherVec = someVec.xxxx + anotherVec.yxzy;

You can use any combination of up to 4 letters to create a new vector (of the same type) as long as the original vector has those components; it is not allowed to access the .z component of a vec2 for example. We can also pass vectors as arguments to different vector constructor calls, reducing the number of arguments required:

vec2 vect = vec2(0.5, 0.7);
vec4 result = vec4(vect, 0.0, 0.0);
vec4 otherResult = vec4(result.xyz, 1.0);

Vectors are thus a flexible datatype that we can use for all kinds of input and output. Throughout the book you'll see plenty of examples of how we can creatively manage vectors.


Ins and outs

Shaders are nice little programs on their own, but they are part of a whole and for that reason we want to have inputs and outputs on the individual shaders so that we can move stuff around. GLSL defined the in and out keywords specifically for that purpose. Each shader can specify inputs and outputs using those keywords and wherever an output variable matches with an input variable of the next shader stage they're passed along. The vertex and fragment shader differ a bit though.


Vectors - swizzling

Vertex - attributes

worldspace (position) transformed matrix to screenspace (projection, modelview, position)

Fragment - vectors

R, G, B, A 
sdf (signed distance function) 2d and 3d shapes in shaders 


#include <GL/glu.h>
#include <GL/glut.h>
#include <vector>
#include <cmath>

const int SCREEN_WIDTH  = 1024;
const int SCREEN_HEIGHT = 1024;
const float camera[]           = {.6,0,1};
const float light0_position[4] = {1,1,1,0};

void render_scene(void) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(camera[0], camera[1], camera[2], 0,  0, 0, 0, 1, 0);
	glColor3f(.8, 0., 0.);
	glutSolidTeapot(.7);
	glutSwapBuffers();
}

void process_keys(unsigned char key, int x, int y) {
	if (27==key) {
		exit(0);
	}
}

void change_size(int w, int h) {
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
    glViewport(0, 0, w, h);
	glOrtho(-1,1,-1,1,-1,8);
	glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	glutCreateWindow("GLSL tutorial");
	glClearColor(0.0,0.0,1.0,1.0);

	glutDisplayFunc(render_scene);
	glutReshapeFunc(change_size);
	glutKeyboardFunc(process_keys);

	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

	glutMainLoop();
	return 0;
}

GLSL tut, [], [], []


raylib[edit | edit source]

  • Made for games and animations
  • Input polled
  • every frame starts from fresh empty screen, you are tracking any state entity data per refresh
  • nothing happens until EndDrawing is called

Game loop - Quit -> Handle Input -> Update Game -> begin Drawing -> Draw Game -> End Drawing -> to Quit

DrawFPS, SetTargetFPS,

Animations, your code covers all motions, use time functions GetFrameTime()

DrawTexturePro (tex2d, src, dest, origin, rot, color)

Camera2d (), beginMode2D , EndMode2D, GetworldToScreen2D (pos, camera) for HUD, health etc, GetScreenToWorld2D (pos, camera) what World items clicked,


iqm model and animation support -> binary file -> resource package

A* (A star) pathfinding


gcc -o game main.c -lraylib 


#include "raylib.h"

int main(void)
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib ");

    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second

    while (!WindowShouldClose())        // Detect window close button or ESC key
    {

        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode2D(camera);


            EndMode2D();

        EndDrawing();
    }


    CloseWindow();        // Close window and OpenGL context

    return 0;
}


#include <stdio.h>
#include "raylib.h"

int main()
{
	Texture2D sprite;
	Sound sound;
	Music music;
	
	InitWindow(800, 450, "basic window");
	
	InitAudioDevice();
	
	sprite = LoadTexture("sprite.png");
	sound = LoadSound("sound.ogg");
	music = LoadMusicStream("music.mp3");
	
	float posX = -sprite.width;
	
	while(!WindowShouldClose())
	{
		UpdateMusicStream(music);
		
		posX += GetFrameTime() * 300;
		if (posX > 800)
		{
			posX = -sprite.width;
		}
		
		if (IsKeyPressed(KEY_SPACE))
		{
			StopMusicStream(music);
			PlayMusicStream(music);
		}
		
		if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
		{
			Rectangle spriteRect = {
				posX,
				10,
				sprite.width,
				sprite.height
			};
			
			if (CheckCollisionPointRec(GetMousePosition(), spriteRect))
			{
				PlaySound(sound);
			}
		}
		
		BeginDrawing();
		
			ClearBackground(RAYWHITE);
			
			DrawTexture(sprite, posX, 10, WHITE);
			// DrawCircle(posX, 10, 5, GREEN);	
			
		EndDrawing();
	}
	
	StopMusicStream(music);
	
	CloseAudioDevice();
	
	CloseWindow();
	
	return 0;
}


/*******************************************************************************************
*
*   raylib [core] example - 2D Camera system
*
*   Example originally created with raylib 1.5, last time updated with raylib 3.0
*
*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
*   BSD-like license that allows static linking with closed source software
*
*   Copyright (c) 2016-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#define MAX_BUILDINGS   100

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");

    Rectangle player = { 400, 280, 40, 40 };
    Rectangle buildings[MAX_BUILDINGS] = { 0 };
    Color buildColors[MAX_BUILDINGS] = { 0 };

    int spacing = 0;

    for (int i = 0; i < MAX_BUILDINGS; i++)
    {
        buildings[i].width = (float)GetRandomValue(50, 200);
        buildings[i].height = (float)GetRandomValue(100, 800);
        buildings[i].y = screenHeight - 130.0f - buildings[i].height;
        buildings[i].x = -6000.0f + spacing;

        spacing += (int)buildings[i].width;

        buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
    }

    Camera2D camera = { 0 };
    camera.target = (Vector2){ player.x + 20.0f, player.y + 20.0f };
    camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
    camera.rotation = 0.0f;
    camera.zoom = 1.0f;

    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        // Player movement
        if (IsKeyDown(KEY_RIGHT)) player.x += 2;
        else if (IsKeyDown(KEY_LEFT)) player.x -= 2;

        // Camera target follows player
        camera.target = (Vector2){ player.x + 20, player.y + 20 };

        // Camera rotation controls
        if (IsKeyDown(KEY_A)) camera.rotation--;
        else if (IsKeyDown(KEY_S)) camera.rotation++;

        // Limit camera rotation to 80 degrees (-40 to 40)
        if (camera.rotation > 40) camera.rotation = 40;
        else if (camera.rotation < -40) camera.rotation = -40;

        // Camera zoom controls
        camera.zoom += ((float)GetMouseWheelMove()*0.05f);

        if (camera.zoom > 3.0f) camera.zoom = 3.0f;
        else if (camera.zoom < 0.1f) camera.zoom = 0.1f;

        // Camera reset (zoom and rotation)
        if (IsKeyPressed(KEY_R))
        {
            camera.zoom = 1.0f;
            camera.rotation = 0.0f;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode2D(camera);

                DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);

                for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);

                DrawRectangleRec(player, RED);

                DrawLine((int)camera.target.x, -screenHeight*10, (int)camera.target.x, screenHeight*10, GREEN);
                DrawLine(-screenWidth*10, (int)camera.target.y, screenWidth*10, (int)camera.target.y, GREEN);

            EndMode2D();

            DrawText("SCREEN AREA", 640, 10, 20, RED);

            DrawRectangle(0, 0, screenWidth, 5, RED);
            DrawRectangle(0, 5, 5, screenHeight - 10, RED);
            DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
            DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);

            DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
            DrawRectangleLines( 10, 10, 250, 113, BLUE);

            DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
            DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
            DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
            DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
            DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}


/*******************************************************************************************
*
*   raylib [core] example - 2D Camera platformer
*
*   Example originally created with raylib 2.5, last time updated with raylib 3.0
*
*   Example contributed by arvyy (@arvyy) and reviewed by Ramon Santamaria (@raysan5)
*
*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
*   BSD-like license that allows static linking with closed source software
*
*   Copyright (c) 2019-2024 arvyy (@arvyy)
*
********************************************************************************************/

#include "raylib.h"
#include "raymath.h"

#define G 400
#define PLAYER_JUMP_SPD 350.0f
#define PLAYER_HOR_SPD 200.0f

typedef struct Player {
    Vector2 position;
    float speed;
    bool canJump;
} Player;

typedef struct EnvItem {
    Rectangle rect;
    int blocking;
    Color color;
} EnvItem;

//----------------------------------------------------------------------------------
// Module functions declaration
//----------------------------------------------------------------------------------
void UpdatePlayer(Player *player, EnvItem *envItems, int envItemsLength, float delta);
void UpdateCameraCenter(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");

    Player player = { 0 };
    player.position = (Vector2){ 400, 280 };
    player.speed = 0;
    player.canJump = false;
    EnvItem envItems[] = {
        {{ 0, 0, 1000, 400 }, 0, LIGHTGRAY },
        {{ 0, 400, 1000, 200 }, 1, GRAY },
        {{ 300, 200, 400, 10 }, 1, GRAY },
        {{ 250, 300, 100, 10 }, 1, GRAY },
        {{ 650, 300, 100, 10 }, 1, GRAY }
    };

    int envItemsLength = sizeof(envItems)/sizeof(envItems[0]);

    Camera2D camera = { 0 };
    camera.target = player.position;
    camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
    camera.rotation = 0.0f;
    camera.zoom = 1.0f;

    // Store pointers to the multiple update camera functions
    void (*cameraUpdaters[])(Camera2D*, Player*, EnvItem*, int, float, int, int) = {
        UpdateCameraCenter,
        UpdateCameraCenterInsideMap,
        UpdateCameraCenterSmoothFollow,
        UpdateCameraEvenOutOnLanding,
        UpdateCameraPlayerBoundsPush
    };

    int cameraOption = 0;
    int cameraUpdatersLength = sizeof(cameraUpdaters)/sizeof(cameraUpdaters[0]);

    char *cameraDescriptions[] = {
        "Follow player center",
        "Follow player center, but clamp to map edges",
        "Follow player center; smoothed",
        "Follow player center horizontally; update player center vertically after landing",
        "Player push camera on getting too close to screen edge"
    };

    SetTargetFPS(60);
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())
    {
        // Update
        //----------------------------------------------------------------------------------
        float deltaTime = GetFrameTime();

        UpdatePlayer(&player, envItems, envItemsLength, deltaTime);

        camera.zoom += ((float)GetMouseWheelMove()*0.05f);

        if (camera.zoom > 3.0f) camera.zoom = 3.0f;
        else if (camera.zoom < 0.25f) camera.zoom = 0.25f;

        if (IsKeyPressed(KEY_R))
        {
            camera.zoom = 1.0f;
            player.position = (Vector2){ 400, 280 };
        }

        if (IsKeyPressed(KEY_C)) cameraOption = (cameraOption + 1)%cameraUpdatersLength;

        // Call update camera function by its pointer
        cameraUpdaters[cameraOption](&camera, &player, envItems, envItemsLength, deltaTime, screenWidth, screenHeight);
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(LIGHTGRAY);

            BeginMode2D(camera);

                for (int i = 0; i < envItemsLength; i++) DrawRectangleRec(envItems[i].rect, envItems[i].color);

                Rectangle playerRect = { player.position.x - 20, player.position.y - 40, 40, 40 };
                DrawRectangleRec(playerRect, RED);
                
                DrawCircle(player.position.x, player.position.y, 5, GOLD);

            EndMode2D();

            DrawText("Controls:", 20, 20, 10, BLACK);
            DrawText("- Right/Left to move", 40, 40, 10, DARKGRAY);
            DrawText("- Space to jump", 40, 60, 10, DARKGRAY);
            DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, DARKGRAY);
            DrawText("- C to change camera mode", 40, 100, 10, DARKGRAY);
            DrawText("Current camera mode:", 20, 120, 10, BLACK);
            DrawText(cameraDescriptions[cameraOption], 40, 140, 10, DARKGRAY);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

void UpdatePlayer(Player *player, EnvItem *envItems, int envItemsLength, float delta)
{
    if (IsKeyDown(KEY_LEFT)) player->position.x -= PLAYER_HOR_SPD*delta;
    if (IsKeyDown(KEY_RIGHT)) player->position.x += PLAYER_HOR_SPD*delta;
    if (IsKeyDown(KEY_SPACE) && player->canJump)
    {
        player->speed = -PLAYER_JUMP_SPD;
        player->canJump = false;
    }

    bool hitObstacle = false;
    for (int i = 0; i < envItemsLength; i++)
    {
        EnvItem *ei = envItems + i;
        Vector2 *p = &(player->position);
        if (ei->blocking &&
            ei->rect.x <= p->x &&
            ei->rect.x + ei->rect.width >= p->x &&
            ei->rect.y >= p->y &&
            ei->rect.y <= p->y + player->speed*delta)
        {
            hitObstacle = true;
            player->speed = 0.0f;
            p->y = ei->rect.y;
            break;
        }
    }

    if (!hitObstacle)
    {
        player->position.y += player->speed*delta;
        player->speed += G*delta;
        player->canJump = false;
    }
    else player->canJump = true;
}

void UpdateCameraCenter(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
    camera->offset = (Vector2){ width/2.0f, height/2.0f };
    camera->target = player->position;
}

void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
    camera->target = player->position;
    camera->offset = (Vector2){ width/2.0f, height/2.0f };
    float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;

    for (int i = 0; i < envItemsLength; i++)
    {
        EnvItem *ei = envItems + i;
        minX = fminf(ei->rect.x, minX);
        maxX = fmaxf(ei->rect.x + ei->rect.width, maxX);
        minY = fminf(ei->rect.y, minY);
        maxY = fmaxf(ei->rect.y + ei->rect.height, maxY);
    }

    Vector2 max = GetWorldToScreen2D((Vector2){ maxX, maxY }, *camera);
    Vector2 min = GetWorldToScreen2D((Vector2){ minX, minY }, *camera);

    if (max.x < width) camera->offset.x = width - (max.x - width/2);
    if (max.y < height) camera->offset.y = height - (max.y - height/2);
    if (min.x > 0) camera->offset.x = width/2 - min.x;
    if (min.y > 0) camera->offset.y = height/2 - min.y;
}

void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
    static float minSpeed = 30;
    static float minEffectLength = 10;
    static float fractionSpeed = 0.8f;

    camera->offset = (Vector2){ width/2.0f, height/2.0f };
    Vector2 diff = Vector2Subtract(player->position, camera->target);
    float length = Vector2Length(diff);

    if (length > minEffectLength)
    {
        float speed = fmaxf(fractionSpeed*length, minSpeed);
        camera->target = Vector2Add(camera->target, Vector2Scale(diff, speed*delta/length));
    }
}

void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
    static float evenOutSpeed = 700;
    static int eveningOut = false;
    static float evenOutTarget;

    camera->offset = (Vector2){ width/2.0f, height/2.0f };
    camera->target.x = player->position.x;

    if (eveningOut)
    {
        if (evenOutTarget > camera->target.y)
        {
            camera->target.y += evenOutSpeed*delta;

            if (camera->target.y > evenOutTarget)
            {
                camera->target.y = evenOutTarget;
                eveningOut = 0;
            }
        }
        else
        {
            camera->target.y -= evenOutSpeed*delta;

            if (camera->target.y < evenOutTarget)
            {
                camera->target.y = evenOutTarget;
                eveningOut = 0;
            }
        }
    }
    else
    {
        if (player->canJump && (player->speed == 0) && (player->position.y != camera->target.y))
        {
            eveningOut = 1;
            evenOutTarget = player->position.y;
        }
    }
}

void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
    static Vector2 bbox = { 0.2f, 0.2f };

    Vector2 bboxWorldMin = GetScreenToWorld2D((Vector2){ (1 - bbox.x)*0.5f*width, (1 - bbox.y)*0.5f*height }, *camera);
    Vector2 bboxWorldMax = GetScreenToWorld2D((Vector2){ (1 + bbox.x)*0.5f*width, (1 + bbox.y)*0.5f*height }, *camera);
    camera->offset = (Vector2){ (1 - bbox.x)*0.5f * width, (1 - bbox.y)*0.5f*height };

    if (player->position.x < bboxWorldMin.x) camera->target.x = player->position.x;
    if (player->position.y < bboxWorldMin.y) camera->target.y = player->position.y;
    if (player->position.x > bboxWorldMax.x) camera->target.x = bboxWorldMin.x + (player->position.x - bboxWorldMax.x);
    if (player->position.y > bboxWorldMax.y) camera->target.y = bboxWorldMin.y + (player->position.y - bboxWorldMax.y);
}
/*
	Raylib example file.
	This is an example main file for a simple  C/C++raylib project.
*/

#include "raylib.h"
#include "raymath.h"


// define a timer
typedef struct
{
    float Lifetime;
}Timer;

// start or restart a timer with a specific lifetime
void StartTimer(Timer* timer, float lifetime)
{
    if (timer != NULL)
        timer->Lifetime = lifetime;
}

// update a timer with the current frame time
void UpdateTimer(Timer* timer)
{
    // subtract this frame from the timer if it's not allready expired
    if (timer != NULL && timer->Lifetime > 0)
        timer->Lifetime -= GetFrameTime();
}

// check if a timer is done.
bool TimerDone(Timer* timer)
{
    if (timer != NULL)
        return timer->Lifetime <= 0;

	return false;
}

int main ()
{
	// set up the window
	InitWindow(1280, 800, "Raylib Timer Example");
	SetTargetFPS(144);

    // setup ball info
    float radius = 20;
    float speed = 400;
    Vector2 pos = { radius, 400 };
    Vector2 dir = { 1,0 };

    float ballLife = 2.0f;

    // a timer for the ball
    Timer ballTimer = { 0 };

	// game loop
	while (!WindowShouldClose())
	{
        // check to see if the user clicked
        if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
        {
            // if they did, move the ball to the current position and restart the timer
            pos = GetMousePosition();
            StartTimer(&ballTimer, ballLife);
        }

        // tick our timer
        UpdateTimer(&ballTimer);

        // if the timer hasn't expired, move the ball
        if (!TimerDone(&ballTimer))
        {
            // move the ball based on the speed and the frame time
            pos = Vector2Add(pos, Vector2Scale(dir, GetFrameTime() * speed));

            if (pos.x > GetScreenWidth() - radius)	// check if we have gone over the right edge, and if so, bounce us
            {
                pos.x = GetScreenWidth() - radius;
                dir.x = -1;
            }
            else if (pos.x < radius)				// check if we have gone over the left edge
            {
                pos.x = radius;
                dir.x = 1;
            }
        }

		// drawing
		BeginDrawing();
		ClearBackground(BLACK);

        // draw the ball where it is if the timer is alive
        if (!TimerDone(&ballTimer))
            DrawCircleV(pos, radius, RED);

		EndDrawing();
	}

	// cleanup
	CloseWindow();
	return 0;
}


#include "raylib.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <math.h>

#define SCALE_FACTOR 1.6

typedef struct {
    int x;
    int y;
    int width;
    int height;
    int velocity;
    int direction;
    int speed;
    bool jumping;
    bool walking;
} Character;

typedef enum {
    PLATFORM,
    FLOOR,
} PlatformType;

typedef struct {
    int x;
    int y;
    int width;
    int height;
    PlatformType type;
} Platform;

float rand_float() {
    return (float) ( (float) rand() / (float) RAND_MAX );
}

int character_on_platform( Character character, Platform platforms[], int count ) {
    for( int i = 0; i < count; i++ ) {
        Rectangle platform_rec = {
            .x = platforms[i].x,
            .y = platforms[i].y,
            .width = platforms[i].width,
            .height = platforms[i].height,
        };

        Rectangle character_rec = {
            .x = character.x + 10,
            .y = character.y + character.height - character.height * 0.2,
            .width = character.width - 15,
            .height = character.height * 0.2 + 1,
        };

        if( CheckCollisionRecs( character_rec, platform_rec ) ) {
            return i;
        }
    }

    return -1;
}

int main() {
    srand(time(NULL));

    int window_width = 800 * SCALE_FACTOR;
    int window_height = 600 * SCALE_FACTOR;

    InitWindow( window_width, window_height, "My Game" );
    SetTargetFPS( 60 );

    Character character = {
        .x = window_width/2,
        .y = window_height/2,
        .width = 101,
        .height = 260,
        .velocity = 4 * SCALE_FACTOR,
        .speed = 6,
        .walking = false,
        .jumping = false,
    };

    int gravity = 2 * SCALE_FACTOR;

    Vector2 camera_offset = {
        .x = 0,
        .y = 0,
    };

    Vector2 camera_target = {
        .x = 0,
        .y = 0,
    };

    Camera2D camera = {
        .offset = camera_offset,
        .target = camera_target,
        .rotation = 0,
        .zoom = 1,
    };

    float platform_spacing = 0.01;

    int world_width = window_width * 10;
    int platform_width = 180;
    int platform_count = world_width / ( platform_width + platform_spacing * window_width );
    int floor_piece_width = 490;
    int floor_piece_height = 190;
    int floor_piece_count = ceil( (float) world_width / (float) floor_piece_width );
    int floor_whitespace = 33;

    int platform_height = 50;
    int platform_min_y = window_height * 0.2;
    int platform_max_y = window_height - floor_piece_height - platform_height - platform_min_y;
    int platform1_whitespace = 45;
    int platform2_whitespace = 20;

    platform_count += floor_piece_count;

    Platform platforms[platform_count+1];

    Texture2D background_texture = LoadTexture( "img/background.png" );
    int background_width = 1792;
    int background_overflow = background_width - window_width;
    float background_ratio = 1 / ((float)(world_width - window_width) / (float)background_overflow);
    int background_x = 0;

    Image platform1_image = LoadImage( "img/platform1.png" );
    Texture2D platform1_texture = LoadTextureFromImage( platform1_image );
    UnloadImage( platform1_image );

    Image platform2_image = LoadImage( "img/platform2.png" );
    Texture2D platform2_texture = LoadTextureFromImage( platform2_image );
    UnloadImage( platform2_image );

    // Create floor
    Image floor_image = LoadImage( "img/floor.png" );
    Texture2D floor_piece_texture = LoadTextureFromImage( floor_image );
    UnloadImage( floor_image );

    int i = 0;
    int floor_x = 0;
    for( ; i < floor_piece_count; i++ ) {
        platforms[i].x = floor_x;
        platforms[i].y = window_height - floor_piece_height + floor_whitespace;
        platforms[i].width = floor_piece_width;
        platforms[i].height = floor_piece_height;
        platforms[i].type = FLOOR;

        floor_x += platforms[i].width;
    }

    int platform_x = window_width * 0.1;
    for( ; i <= platform_count; i++ ) {
        platforms[i].x = platform_x;
        platforms[i].y = rand_float() * platform_max_y + platform_min_y;
        platforms[i].width = platform_width;
        platforms[i].height = platform_height;
        platforms[i].type = PLATFORM;

        platform_x += platforms[i].width + window_width * platform_spacing;
    }

    Image char_stand_img = LoadImage( "img/standing.png" );
    Texture2D char_stand_right = LoadTextureFromImage( char_stand_img );
    ImageFlipHorizontal( &char_stand_img );
    Texture2D char_stand_left = LoadTextureFromImage( char_stand_img );
    UnloadImage( char_stand_img );

    Image char_jump_img = LoadImage( "img/jumping.png" );
    Texture2D char_jump_right = LoadTextureFromImage( char_jump_img );
    ImageFlipHorizontal( &char_jump_img );
    Texture2D char_jump_left = LoadTextureFromImage( char_jump_img );
    UnloadImage( char_jump_img );

    Image char_walk1_img = LoadImage( "img/walk1.png" );
    Texture2D char_walk1_right = LoadTextureFromImage( char_walk1_img );
    ImageFlipHorizontal( &char_walk1_img );
    Texture2D char_walk1_left = LoadTextureFromImage( char_walk1_img );
    UnloadImage( char_walk1_img );

    Image char_walk2_img = LoadImage( "img/walk2.png" );
    Texture2D char_walk2_right = LoadTextureFromImage( char_walk2_img );
    ImageFlipHorizontal( &char_walk2_img );
    Texture2D char_walk2_left = LoadTextureFromImage( char_walk2_img );
    UnloadImage( char_walk2_img );

    while( ! WindowShouldClose() ) {
        BeginDrawing();

        character.walking = false;

        if( character.x > window_width * 0.6 ) {
            camera.offset.x = -(character.x - window_width * 0.6);

        } else if( character.x < window_width * 0.4 ) {
            camera.offset.x = -(character.x - window_width * 0.4);
        }

        if( camera.offset.x > 0 ) {
            camera.offset.x = 0;
        }

        if( camera.offset.x < -(world_width - window_width) ) {
            camera.offset.x = -(world_width - window_width);
        }

        BeginMode2D( camera );

        background_x = -camera.offset.x;
        background_x -= background_x * background_ratio;

        character.y += character.velocity;
        character.velocity += gravity;

        int current_platform = character_on_platform(
            character, platforms, platform_count
        );

        if( current_platform != -1 ) {
            if( character.velocity > 0 ) {
                character.y = platforms[current_platform].y - character.height;
                character.velocity = 0;
                character.jumping = false;
            }

            if( IsKeyPressed( KEY_SPACE ) ) {
                character.velocity = -30 * SCALE_FACTOR;
                character.jumping = true;
            }
        }

        if( IsKeyDown( KEY_LEFT ) ) {
            character.walking = true;
            character.x -= character.speed * SCALE_FACTOR;
            character.direction = -1;
        }

        if( IsKeyDown( KEY_RIGHT ) ) {
            character.walking = true;
            character.x += character.speed * SCALE_FACTOR;
            character.direction = 1;
        }

        ClearBackground( WHITE );

        DrawTexture( background_texture, background_x, 0, WHITE );

        for( int i = 0; i < platform_count; i++ ) {
            if( platforms[i].type == FLOOR ) {
                DrawTexture( floor_piece_texture, platforms[i].x, platforms[i].y - floor_whitespace, WHITE );
            } else {
                Texture2D platform_texture = platform1_texture;
                int whitespace = platform1_whitespace;
                if( i % 2 == 0 ) {
                    platform_texture = platform2_texture;
                    whitespace = platform2_whitespace;
                }
                DrawTexture( platform_texture, platforms[i].x - 10, platforms[i].y - whitespace, WHITE );
            }
        }

        Texture2D char_texture;
        if( character.jumping ) {
            if( character.direction == -1 ) {
                char_texture = char_jump_left;
            } else {
                char_texture = char_jump_right;
            }
        } else if( character.walking ) {
            double time = GetTime() * 10;
            if( character.direction == -1 ) {
                if( ((int) time) % 2 == 0 ) {
                    char_texture = char_walk1_left;
                } else {
                    char_texture = char_walk2_left;
                }
            } else {
                if( ((int) time) % 2 == 0 ) {
                    char_texture = char_walk1_right;
                } else {
                    char_texture = char_walk2_right;
                }
            }
        } else {
            if( character.direction == -1 ) {
                char_texture = char_stand_left;
            } else {
                char_texture = char_stand_right;
            }
        }

        if( character.x < 0 ) {
            character.x = 0;
        }

        if( character.x > world_width - character.width ) {
            character.x = world_width - character.width;
        }

        DrawTexture( char_texture, character.x, character.y, WHITE );

        EndMode2D();
        EndDrawing();
    }

    UnloadTexture( char_stand_right );
    UnloadTexture( char_stand_left );
    UnloadTexture( char_jump_right );
    UnloadTexture( char_jump_left );
    UnloadTexture( char_walk1_right );
    UnloadTexture( char_walk1_left );
    UnloadTexture( char_walk2_right );
    UnloadTexture( char_walk2_left );
    UnloadTexture( floor_piece_texture );
    UnloadTexture( platform1_texture );
    UnloadTexture( platform2_texture );
    UnloadTexture( background_texture );

    return 0;
}


// Raylib Minesweeper
// Andrew Hamel Codes
// https://github.com/AndrewHamel111/raylib-minesweeper
// https://www.youtube.com/watch?v=3-EYrPRdjp4

#include <stdlib.h>
#include <time.h>

#include "raylib.h"
#include "raymath.h"

#define COLS 15
#define ROWS 15

const int screenWidth = 600;
const int screenHeight = 600;

const int cellWidth = screenWidth / COLS;
const int cellHeight = screenHeight / ROWS;

const char* youLose = "YOU LOSE!";
const char* youWin = "YOU WIN!";
const char* pressRToRestart = "Press 'r' to play again!";

typedef struct Cell
{
	int i;
	int j;
	bool containsMine;
	bool revealed;
	bool flagged;
	int nearbyMines;
} Cell;

Cell grid[COLS][ROWS];

Texture2D flagSprite;
int tilesRevealed;
int minesPresent;

typedef enum GameState
{
	PLAYING,
	LOSE,
	WIN
} GameState;

GameState state;

float timeGameStarted;
float timeGameEnded;

void CellDraw(Cell);
bool IndexIsValid(int, int);
void CellReveal(int, int);
void CellFlag(int, int);
int CellCountMines(int, int);
void GridInit(void);
void GridFloodClearFrom(int, int);
void GameInit(void);

int main()
{
	srand(time(0));

	InitWindow(screenWidth, screenHeight, "Raylib Minesweeper by Andrew Hamel");

	flagSprite = LoadTexture("resources/flag.png");

	GameInit();
	
	while(!WindowShouldClose())
	{
		if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
		{
			Vector2 mPos = GetMousePosition();
			int indexI = mPos.x / cellWidth;
			int indexJ = mPos.y / cellHeight;

			if (state == PLAYING && IndexIsValid(indexI, indexJ))
			{
				CellReveal(indexI, indexJ);
			}
		}
		else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
		{
			Vector2 mPos = GetMousePosition();
			int indexI = mPos.x / cellWidth;
			int indexJ = mPos.y / cellHeight;

			if (state == PLAYING && IndexIsValid(indexI, indexJ))
			{
				CellFlag(indexI, indexJ);
			}
		}

		if (IsKeyPressed(KEY_R))
		{
			GameInit();
		}

		BeginDrawing();

			ClearBackground(RAYWHITE);
			
			for (int i = 0; i < COLS; i++)
			{
				for (int j = 0; j < ROWS; j++)
				{
					CellDraw(grid[i][j]);
				}
			}

			if (state == LOSE)
			{
				DrawRectangle(0, 0, screenWidth,screenHeight, Fade(WHITE, 0.8f));
				DrawText(youLose, screenWidth / 2 - MeasureText(youLose, 40) / 2, screenHeight / 2 - 10, 40, DARKGRAY);
				DrawText(pressRToRestart, screenWidth / 2 - MeasureText(pressRToRestart, 20) / 2, screenHeight * 0.75f - 10, 20, DARKGRAY);

				int minutes = (int)(timeGameEnded - timeGameStarted) / 60;
				int seconds = (int)(timeGameEnded - timeGameStarted) % 60;
				DrawText(TextFormat("Time played: %d minutes, %d seconds.", minutes, seconds), 20, screenHeight - 40, 20, DARKGRAY);
			}

			if (state == WIN)
			{
				DrawRectangle(0, 0, screenWidth,screenHeight, Fade(WHITE, 0.8f));
				DrawText(youWin, screenWidth / 2 - MeasureText(youWin, 40) / 2, screenHeight / 2 - 10, 40, DARKGRAY);
				DrawText(pressRToRestart, screenWidth / 2 - MeasureText(pressRToRestart, 20) / 2, screenHeight * 0.75f - 10, 20, DARKGRAY);

				int minutes = (int)(timeGameEnded - timeGameStarted) / 60;
				int seconds = (int)(timeGameEnded - timeGameStarted) % 60;
				DrawText(TextFormat("Time played: %d minutes, %d seconds.", minutes, seconds), 20, screenHeight - 40, 20, DARKGRAY);
			}

		EndDrawing();
	}
	
	CloseWindow();
	
	return 0;
}

void CellDraw(Cell cell)
{
	if (cell.revealed)
	{
		if (cell.containsMine)
		{
			DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, RED);
		}
		else
		{
			DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, LIGHTGRAY);

			if (cell.nearbyMines > 0)
			{
				DrawText(TextFormat("%d", cell.nearbyMines), cell.i * cellWidth + 12, cell.j * cellHeight + 4, cellHeight - 8, DARKGRAY);
			}
		}
	}
	else if (cell.flagged)
	{
		// draw flag
		Rectangle source = {0, 0, flagSprite.width, flagSprite.height};
		Rectangle dest = {cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight};
		Vector2 origin = {0, 0};

		DrawTexturePro(flagSprite, source, dest, origin, 0.0f, Fade(WHITE, 0.5f));
	}

	DrawRectangleLines(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, BLACK);
}

bool IndexIsValid(int i, int j)
{
	return i >= 0 && i < COLS && j >= 0 && j < ROWS;
}

void CellReveal(int i, int j)
{
	if (grid[i][j].flagged || grid[i][j].revealed)
	{
		return;
	}

	grid[i][j].revealed = true;

	if (grid[i][j].containsMine)
	{
		state = LOSE;
		timeGameEnded = GetTime();
	}
	else
	{
		if (grid[i][j].nearbyMines == 0)
		{
			GridFloodClearFrom(i, j);
		}

		tilesRevealed++;

		if (tilesRevealed >= ROWS * COLS - minesPresent)
		{
			state = WIN;
			timeGameEnded = GetTime();
		}
	}
}

void CellFlag(int i, int j)
{
	if (grid[i][j].revealed)
	{
		return;
	}

	grid[i][j].flagged = !grid[i][j].flagged;
}

int CellCountMines(int i, int j)
{
	int count = 0;
	for (int iOff = -1; iOff <= 1; iOff++)
	{
		for (int jOff = -1; jOff <= 1; jOff++)
		{
			if (iOff == 0 && jOff == 0)
			{
				continue;
			}

			if (!IndexIsValid(i + iOff, j + jOff))
			{
				continue;
			}

			if (grid[i + iOff][j + jOff].containsMine)
			{
				count++;
			}
		}
	}

	return count;
}

void GridInit(void)
{
	for (int i = 0; i < COLS; i++)
	{
		for (int j = 0; j < ROWS; j++)
		{
			grid[i][j] = (Cell)
			{
				.i = i,
				.j = j,
				.containsMine = false,
				.revealed = false,
				.flagged = false,
				.nearbyMines = -1
			};
		}
	}

	minesPresent = (int)(ROWS * COLS * 0.1f);
	int minesToPlace = minesPresent;
	while (minesToPlace > 0)
	{
		int i = rand() % COLS;
		int j = rand() % ROWS;

		if (!grid[i][j].containsMine)
		{
			grid[i][j].containsMine = true;
			minesToPlace--;
		}
	}

	for (int i = 0; i < COLS; i++)
	{
		for (int j = 0; j < ROWS; j++)
		{
			if (!grid[i][j].containsMine)
			{
				grid[i][j].nearbyMines = CellCountMines(i, j);
			}
		}
	}
}

void GridFloodClearFrom(int i, int j)
{
	for (int iOff = -1; iOff <= 1; iOff++)
	{
		for (int jOff = -1; jOff <= 1; jOff++)
		{
			if (iOff == 0 && jOff == 0)
			{
				continue;
			}

			if (!IndexIsValid(i + iOff, j + jOff))
			{
				continue;
			}

			CellReveal(i + iOff, j + jOff);
		}
	}
}

void GameInit(void)
{
	GridInit();
	state = PLAYING;
	tilesRevealed = 0;
	timeGameStarted = GetTime();
}




C game template, game premake, Asteroids in C raylib, Catch items in C, [],

C++ RPG simple, C++ RPG simple, [],


RLGL[edit | edit source]

#include <raylib.h>

int main() {
    InitWindow(1280, 720, "Model Loading");
    
    Model model = LoadModel("Downloads/LOD0.obj");
    Texture2D tex = LoadTexture("Downloads/Thing.png");
    model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = tex;
    
    Camera cam = {0};
    cam.position = (Vector3){50.0f,50.0f,50.0f};
    cam.target = (Vector3){0.0f,0.0f,0.0f};
    cam.up = (Vector3){0.0f,1.0f,0.0f};
    cam.fovy = 90.f;
    cam.projection = CAMERA_PERSPECTIVE;
    
    Vector3 pos = {0.0f,0.0f,0.0f};
    Vector3 pos2 = {200.0f,1.0f,0.0f};
    BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]);
    
    SetTargetFPS(60);
    
    SetCameraMode(cam, CAMERA_THIRD_PERSON);
    
    while(!WindowShouldClose()) {
        UpdateCamera(&cam);
        BeginDrawing();
        ClearBackground(RAYWHITE);
        BeginMode3D(cam);
        DrawModel(model, pos, 1.0f, WHITE);
        DrawModel(model, pos2, 1.0f, WHITE);
        DrawGrid(20, 10.0f);
        DrawBoundingBox(bounds, GREEN);
        EndMode3D();
        DrawText("Loading obj file", 10, GetScreenHeight()-25, 25, DARKGRAY);
        DrawFPS(10,10);
        EndDrawing();
    }
    
    UnloadTexture(tex);
    UnloadModel(model);
    CloseWindow();
    return 0;
    
}


/*******************************************************************************************
*
*   raylib [models] example - rlgl module usage with push/pop matrix transformations
*
*   NOTE: This example uses [rlgl] module functionality (pseudo-OpenGL 1.1 style coding)
*
*   Example originally created with raylib 2.5, last time updated with raylib 4.0
*
*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
*   BSD-like license that allows static linking with closed source software
*
*   Copyright (c) 2018-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"
#include "rlgl.h"

#include <math.h>           // Required for: cosf(), sinf()

//------------------------------------------------------------------------------------
// Module Functions Declaration
//------------------------------------------------------------------------------------
void DrawSphereBasic(Color color);      // Draw sphere without any matrix transformation

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    const float sunRadius = 4.0f;
    const float earthRadius = 0.6f;
    const float earthOrbitRadius = 8.0f;
    const float moonRadius = 0.16f;
    const float moonOrbitRadius = 1.5f;

    InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl module usage with push/pop matrix transformations");

    // Define the camera to look into our 3d world
    Camera camera = { 0 };
    camera.position = (Vector3){ 16.0f, 16.0f, 16.0f }; // Camera position
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
    camera.fovy = 45.0f;                                // Camera field-of-view Y
    camera.projection = CAMERA_PERSPECTIVE;             // Camera projection type

    float rotationSpeed = 0.2f;         // General system rotation speed

    float earthRotation = 0.0f;         // Rotation of earth around itself (days) in degrees
    float earthOrbitRotation = 0.0f;    // Rotation of earth around the Sun (years) in degrees
    float moonRotation = 0.0f;          // Rotation of moon around itself
    float moonOrbitRotation = 0.0f;     // Rotation of moon around earth in degrees

    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        UpdateCamera(&camera, CAMERA_ORBITAL);

        earthRotation += (5.0f*rotationSpeed);
        earthOrbitRotation += (365/360.0f*(5.0f*rotationSpeed)*rotationSpeed);
        moonRotation += (2.0f*rotationSpeed);
        moonOrbitRotation += (8.0f*rotationSpeed);
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                rlPushMatrix();
                    rlScalef(sunRadius, sunRadius, sunRadius);          // Scale Sun
                    DrawSphereBasic(GOLD);                              // Draw the Sun
                rlPopMatrix();

                rlPushMatrix();
                    rlRotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f);    // Rotation for Earth orbit around Sun
                    rlTranslatef(earthOrbitRadius, 0.0f, 0.0f);         // Translation for Earth orbit

                    rlPushMatrix();
                        rlRotatef(earthRotation, 0.25, 1.0, 0.0);       // Rotation for Earth itself
                        rlScalef(earthRadius, earthRadius, earthRadius);// Scale Earth

                        DrawSphereBasic(BLUE);                          // Draw the Earth
                    rlPopMatrix();

                    rlRotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f);     // Rotation for Moon orbit around Earth
                    rlTranslatef(moonOrbitRadius, 0.0f, 0.0f);          // Translation for Moon orbit
                    rlRotatef(moonRotation, 0.0f, 1.0f, 0.0f);          // Rotation for Moon itself
                    rlScalef(moonRadius, moonRadius, moonRadius);       // Scale Moon

                    DrawSphereBasic(LIGHTGRAY);                         // Draw the Moon
                rlPopMatrix();

                // Some reference elements (not affected by previous matrix transformations)
                DrawCircle3D((Vector3){ 0.0f, 0.0f, 0.0f }, earthOrbitRadius, (Vector3){ 1, 0, 0 }, 90.0f, Fade(RED, 0.5f));
                DrawGrid(20, 1.0f);

            EndMode3D();

            DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, MAROON);
            DrawFPS(10, 10);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

//--------------------------------------------------------------------------------------------
// Module Functions Definitions (local)
//--------------------------------------------------------------------------------------------

// Draw sphere without any matrix transformation
// NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
void DrawSphereBasic(Color color)
{
    int rings = 16;
    int slices = 16;

    // Make sure there is enough space in the internal render batch
    // buffer to store all required vertex, batch is reseted if required
    rlCheckRenderBatchLimit((rings + 2)*slices*6);

    rlBegin(RL_TRIANGLES);
        rlColor4ub(color.r, color.g, color.b, color.a);

        for (int i = 0; i < (rings + 2); i++)
        {
            for (int j = 0; j < slices; j++)
            {
                rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
                           sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
                           cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
                rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
                           sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
                           cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
                rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)),
                           sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
                           cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices)));

                rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
                           sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
                           cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
                rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)),
                           sinf(DEG2RAD*(270+(180/(rings + 1))*(i))),
                           cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices)));
                rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
                           sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
                           cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
            }
        }
    rlEnd();
}


References[edit | edit source]

slowdown when running multiple 3d apps: this happens because of the GL semaphore "ping pong" between the tasks which can cause like >10000 task switches (and GL context switches) per second.

Semaphores are evil! It you have two or more tasks (3d apps) competing for sem then they get into ping pong state when a task is preempted while he owns the sem which is very likely. From then on (until a task switch happens while the sem is *not* locked) what will happen is that only one (GL) call can be made by task #1, then task switch to task #2 happens, which also can only make one (GL) call, then task switch to task #1 happens, which can only make one (GL) call, then task switch to task #2 happens, etc.

One solution to prevent this would be to have some GrabGL/UngrabGL (or ObtainGL,ReleaseGL if that sounds better) functions to be used in 3D apps and then enclose big parts of GL rendering function calls with it. GrabGL would lock the GL sem and make sure context is correct. ReleaseGL would unlock the GL sem. Even better would be if GrabGL switched all the GL functions (through function table) to versions which which don't use semlock/contextcheck/semunlock at all.

you must pass SDL_OPENGL to SDL_SetVideoMode, you must specify several GL attributes (depth buffer size, framebuffer sizes) using SDL_GL_SetAttribute and finally, if you wish to use double buffering you must specify it as a GL attribute, not by passing the SDL_DOUBLEBUF flag to SDL_SetVideoMode
using a double-buffered display, then you must use SDL_GL_SwapBuffers() to swap the buffers and update the display. To request double-buffering with OpenGL, use SDL_GL_SetAttribute with SDL_GL_DOUBLEBUFFER, and use SDL_GL_GetAttribute to see if you actually got

Loaders may be required to help with different versions needs Lua based loading libraries with extensions, extensions,


GL_VERSION: 1.4 Mesa 7.11
GL_EXTENSIONS: GL_ARB_multisample GL_EXT_abgr GL_EXT_bgra GL_EXT_blend_color GL_EXT_blend_logic_op GL_EXT_blend_minmax GL_EXT_blend_subtract GL_EXT_copy_texture GL_EXT_polygon_offset GL_EXT_subtexture GL_EXT_texture_object GL_EXT_vertex_array GL_EXT_compiled_vertex_array GL_EXT_texture GL_EXT_texture3D GL_IBM_rasterpos_clip GL_ARB_point_parameters GL_EXT_draw_range_elements GL_EXT_packed_pixels GL_EXT_point_parameters GL_EXT_rescale_normal GL_EXT_separate_specular_color GL_EXT_texture_edge_clamp GL_SGIS_generate_mipmap GL_SGIS_texture_border_clamp GL_SGIS_texture_edge_clamp GL_SGIS_texture_lod GL_ARB_multitexture GL_IBM_multimode_draw_arrays GL_IBM_texture_mirrored_repeat GL_ARB_texture_cube_map GL_ARB_texture_env_add GL_ARB_transpose_matrix GL_EXT_blend_func_separate GL_EXT_fog_coord GL_EXT_multi_draw_arrays GL_EXT_secondary_color GL_EXT_texture_env_add GL_EXT_texture_filter_anisotropic GL_EXT_texture_lod_bias GL_INGR_blend_func_separate GL_NV_blend_square GL_NV_light_max_exponent GL_NV_texgen_reflection GL_NV_texture_env_combine4 GL_SUN_multi_draw_arrays GL_ARB_texture_border_clamp GL_ARB_texture_compression GL_EXT_framebuffer_object GL_EXT_texture_env_dot3 GL_MESA_window_pos GL_NV_packed_depth_stencil GL_NV_texture_rectangle GL_ARB_depth_texture GL_ARB_shadow GL_ARB_texture_env_combine GL_ARB_texture_env_crossbar GL_ARB_texture_env_dot3 GL_ARB_texture_mirrored_repeat GL_ARB_window_pos GL_EXT_stencil_two_side GL_EXT_texture_cube_map GL_APPLE_packed_pixels GL_APPLE_vertex_array_object GL_ARB_draw_buffers GL_ARB_fragment_program GL_ARB_vertex_program GL_ATI_draw_buffers GL_ATI_texture_env_combine3 GL_EXT_shadow_funcs GL_EXT_stencil_wrap GL_MESA_pack_invert GL_MESA_ycbcr_texture GL_NV_primitive_restart GL_ARB_fragment_program_shadow GL_ARB_half_float_pixel GL_ARB_point_sprite GL_ARB_sync GL_ARB_texture_non_power_of_two GL_ARB_vertex_buffer_object GL_OES_read_format GL_ARB_color_buffer_float GL_ARB_pixel_buffer_object GL_ARB_texture_rectangle GL_EXT_pixel_buffer_object GL_EXT_texture_rectangle GL_ARB_framebuffer_object GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample GL_EXT_packed_depth_stencil GL_ARB_vertex_array_object GL_ATI_separate_stencil GL_EXT_gpu_program_parameters GL_EXT_texture_env_combine GL_OES_EGL_image GL_ARB_copy_buffer GL_ARB_map_buffer_range GL_ARB_vertex_array_bgra GL_EXT_vertex_array_bgra GL_ARB_draw_elements_base_vertex GL_ARB_fragment_coord_conventions GL_ARB_provoking_vertex GL_ARB_sampler_objects GL_EXT_provoking_vertex GL_ARB_robustness
GL_RENDERER: Gallium 0.4 on i915 (chipset: 915GM)
GL_VENDOR: VMware, Inc.
GLU_VERSION: 1.3
GLU_EXTENSIONS: GLU_EXT_nurbs_tessellator GLU_EXT_object_space_tess
GLUT_API_VERSION: 5
GLUT_XLIB_IMPLEMENTATION: 15