reni2D library - (added shader support,pixel perfect collision)

Started by
2 comments, last by lvnt 13 years, 6 months ago
Hi,
I'd like to share the 2D library we started using at our college for beginner and advanced programming students.
Students find it easy to use and seem to get quick results.
It is written in C using OpenGL,

It is very new, I update it quite often, download it from here for newer versions
http://involuntaryexercise.com/apps/reni2d.zip

added: very easy to use pixel shader support, see example code
added: fast pixel perfect collision detection
check out example code

feature list:
- very clean and straight forward api
- simple and plain state machine
- no classes, no inheritance, no custom data types, no constants

- hardware accelerated 2D sprites
- pixel perfect collision detection (can be batched and baked)
- easy to use pixel shaders
- alpha blending and tinting
- texture sampling operations (easy tiles,sprite/font sheets,sliding)
- mouse and keyboard input (including hardware polled mouse movements)

- random numbers, rotation operations, bitmap loading


edit:
I finally tested reni2D's performance against something out there.
I hear a lot about Haaf's engine, so I took his "Thousands of Hares" example and replaced drawing calls with reni2D. Test app is running 2000 sprites by default, "SPACE KEY" flips between blending mode 0 and 2.
http://hge.relishgames.com/doc/html/tutorials_tut07.html
It performed great, even slightly faster. Here is the test executable,images and the ported example code:
http://involuntaryexercise.com/apps/reni2dhgetest.zip
mode 0

mode 2


here is all the code needed to create the following effect:
unsigned int s=raGetShader("raColor.xyz = raArg0.z*raColor/distance(raArg0.xy,raPixelPos.xy); ");raSetShader(s);


shader test screen shots (20000 sprites!)- lighting




here is the entire code for the example image:


#include "reni2D.h"int RA_MAIN() {	unsigned int betty = 0;	float x,y;        float a=0;	//init engine	raInit(800,600);	//load sampler	betty = raGetSamplerFromFile("betty.bmp");	raSetSampler(betty);	//white opaque color multiplier	raSetDrawColor(255,255,255,255);		while(raRun()) {		raClear();		//draw 64x64 quad		raSetQuadSize(32,32);		raSetQuadRot(-90);		//big betty, at center, rotated -90 around origin		raDrawQuad(400,300);		//using dummy point to do pivot rotation of 45 degress 50 units from origin		raResetDummy();		raMoveDummy(50,0);		raRotateDummy(a);		a+=0.5;		//resulting coordinates are based on 0,0 , now move the altered dummy to center of screen		raMoveDummy(400,300);		//get the results		x = raGetDummyX();		y = raGetDummyY();		//small betty		//draw 16x16 quad		raSetQuadSize(8,8);		raSetQuadRot(45);		raDrawQuad(x,y);	}	}




If you have time, please give it a try and throw some feedback,
Thanks
Levent

(edit4: api overhaul reflected in example code)

[Edited by - lvnt on November 2, 2010 5:29:58 PM]
Advertisement
very good!
Thanks!
What I'm wondering about this code, is if the canvas on a sprite is in the exact middle or one of the corners. I'm asking this in order to try and make a collision algorithm that can detect both horizontal and vertical at once.
Hi, let me try to give you an example,

When you issue the following:
raSetQuadSize(32,32);raDrawQuad(400,300);

you are drawing a 64x64 sprite, centered at 400,300. (using half width and height)

coordinates for vertices in CCW order are:

TOP_LEFT : 400-32,300+32
BOTTOM_LEFT : 400-32,300-32
BOTTOM_RIGHT: 400+32,300-32
TOP_RIGHT : 400+32,300+32

more info and detail:

very basic sprite example
//example sprite class//not including texture worktypedef struct _spr {  float x,y,r,w,h;}spr;//draws sprite centered at canvas, rotated around own originvoid sprDraw(spr *s) {  raSetQuadSize(s->w,s->h);  raSetQuadRot(s->r);  raDrawQuad(s->x,s->y);}


sprite using single texture
//example sprite class//sprite has own texturetypedef struct _spr {  float x,y,r,w,h;  unsigned int tex;}spr;//draws sprite centered at canvas, rotated around own originvoid sprDraw(spr *s) {  raSetSampler(s->tex);  raSetQuadSize(s->w,s->h);  raSetQuadRot(s->r);  raDrawQuad(s->x,s->y);}


sprite using sprite sheet

//example sprite class//sprite uses sprite sheettypedef struct _spr {  float x,y,r,w,h;  float u,v,uw,uh;}spr;//draws sprite centered at canvas, rotated around own origin//assume sprite sheet (sampler) is set before drawing set of sprites/animationsvoid sprDraw(spr *s) {  raSetSamplerArea(s->u,s->w,s->uw,s->uh);  raSetQuadSize(s->w,s->h);  raSetQuadRot(s->r);  raDrawQuad(s->x,s->y);}


[Edited by - lvnt on October 24, 2010 4:52:15 AM]

This topic is closed to new replies.

Advertisement