Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 119 additions & 112 deletions examples/duckdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,136 +5,143 @@
** For more information on Plush, see http://nullsoft.home.ml.org/plush/
*/

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

#include <plush/plush.h>

#include "ex.h"

pl_Mat *Material1, *Material2; // Materials for the duck
pl_Cam *Camera; // Camera
pl_Obj *Object; // The duck
pl_Light *Light; // Light source

uint8_t framebuffer[W*H]; // Our framebuffer to render to

void SetUpColors();
static pl_Mat *Material1, *Material2; // Materials for the duck
static pl_Cam *Camera; // Camera
static pl_Obj *Object; // The duck
static pl_Light *Light; // Light source

int main(int argc, char **argv) {
float *zbuffer; // Our zbuffer
static uint8_t framebuffer[W*H]; // Our framebuffer to render to
static float *zbuffer; // Our zbuffer

printf("%s\n%s\n\n",plVersionString,plCopyrightString); // I like to do this
static void SetUpColors(void);

exSetGraphics(); // Set graphics
int exInit(void **appstate, int argc, char **argv)
{
printf("%s\n%s\n\n",plVersionString,plCopyrightString); // I like to do this

Material1 = plMatCreate();
Material2 = plMatCreate();
Material1 = plMatCreate();
Material2 = plMatCreate();

SetUpColors(); // Init palette
SetUpColors(); // Init palette

Object = plRead3DSObj("duckdemo.3ds",Material1);
if (!Object) {
perror("Can't load duckdemo.3ds");
return 1;
}
Object = plRead3DSObj("duckdemo.3ds",Material1);
if (!Object)
{
printf("Can't load duckdemo.3ds\n");
return PL_EXIT_FAILURE;
}

// First child is an eye, child of child is eye
plMdlSetMat(Object->Children->Model,Material2);
plMdlSetMat(Object->Children->Children->Model,Material2);
// Child of eye is other eye, make it child of duck
plObjAddChild(Object, plObjRemoveParent(Object->Children->Children));
// First child is an eye, child of child is eye
plMdlSetMat(Object->Children->Model,Material2);
plMdlSetMat(Object->Children->Children->Model,Material2);
// Child of eye is other eye, make it child of duck
plObjAddChild(Object, plObjRemoveParent(Object->Children->Children));

plMdlScale(Object->Model,0.1); // Scale object down...
plMdlScale(Object->Model,0.1); // Scale object down...

Object->BackfaceCull = 0; // We want to be able to see through the duck
Object->BackfaceIllumination = 1;
Object->BackfaceCull = 0; // We want to be able to see through the duck
Object->BackfaceIllumination = 1;

Light = plLightCreate(); // Create a lightsource
plLightSet(Light,PL_LIGHT_VECTOR,0,0,0,1.0,1.0); // Vector light, 1.0 intensity
Light = plLightCreate(); // Create a lightsource
plLightSet(Light,PL_LIGHT_VECTOR,0,0,0,1.0,1.0); // Vector light, 1.0 intensity

if ((argc > 1 && !strcmp(argv[1],"-nozb")) ||
(argc > 2 && !strcmp(argv[2],"-nozb"))) zbuffer = 0;
else
zbuffer = (float *) plMalloc(sizeof(float)*W*H);
if ((argc > 1 && !strcmp(argv[1],"-nozb")) ||
(argc > 2 && !strcmp(argv[2],"-nozb"))) zbuffer = 0;
else
zbuffer = (float *) plMalloc(sizeof(float)*W*H);

Camera = plCamCreate(W,H, // Create camera
W*3.0/(H*4.0), // Aspect ratio (usually 1.0)
80.0, framebuffer, zbuffer);
Camera->Z = -500; // move the camera back a bit
if (zbuffer) Camera->Sort = 0; // Sorting not necessary w/ zbuffer
else Camera->Sort = 1;
Camera = plCamCreate(W,H, // Create camera
W*3.0/(H*4.0), // Aspect ratio (usually 1.0)
80.0, framebuffer, zbuffer);
Camera->Z = -500; // move the camera back a bit
if (zbuffer) Camera->Sort = 0; // Sorting not necessary w/ zbuffer
else Camera->Sort = 1;
return PL_EXIT_CONTINUE;
}

while (!exGetKey()) {
Object->Xa += 2.0; // Rotate object
Object->Ya += 2.0;
Object->Za += 2.0;
int exIterate(void *appstate)
{
Object->Xa += 2.0; // Rotate object
Object->Ya += 2.0;
Object->Za += 2.0;

plMemSet(framebuffer, 0, sizeof(framebuffer)); // clear framebuffer & zbuffer
if (Camera->zBuffer)
plMemSet(Camera->zBuffer,0,sizeof(float)*Camera->ScreenWidth*Camera->ScreenHeight);

plRenderBegin(Camera); // Render to camera
plRenderLight(Light); // Render light
plRenderObj(Object); // Render duck
plRenderEnd(); // Finish rendering
plMemCpy(exGraphMem,framebuffer,sizeof(framebuffer)); // dump to screen
return PL_EXIT_CONTINUE;
}

memset(framebuffer, 0, sizeof(framebuffer)); // clear framebuffer & zbuffer
if (Camera->zBuffer) memset(Camera->zBuffer,0,sizeof(float)*
Camera->ScreenWidth*Camera->ScreenHeight);
int exKeyEvent(void *appstate, int key)
{
// any keypress will trigger an exit
return PL_EXIT_SUCCESS;
}

plRenderBegin(Camera); // Render to camera
plRenderLight(Light); // Render light
plRenderObj(Object); // Render duck
plRenderEnd(); // Finish rendering
exWaitVSync(); // Sync with retrace
memcpy(exGraphMem,framebuffer,sizeof(framebuffer)); // dump to screen
}
plObjDelete(Object); // Free duck
plLightDelete(Light); // Free light
plCamDelete(Camera); // Free camera
void exQuit(void *appstate, int code)
{
plObjDelete(Object); // Free duck
plLightDelete(Light); // Free light
plCamDelete(Camera); // Free camera
plFree(zbuffer);
}

plFree(zbuffer);
exSetText(); // Restore text mode
//printf("Try \"duckdemo 640x480\" or \"duckdemo 320x200 -nozb\" etc\n");
return 0;
static void SetUpColors(void)
{
uint8_t pal[768]; // Our rgb triplet palette
pl_Mat *AllMaterials[2];
memset(pal,0,768);

//Material1->Priority = 0; // setup material 1
Material1->NumGradients = 200;
Material1->Diffuse[0] = 203;
Material1->Diffuse[1] = 212;
Material1->Diffuse[2] = 0;
Material1->Specular[0] = 128;
Material1->Specular[1] = 56;
Material1->Specular[2] = 0;
Material1->Shininess = 15;
Material1->Ambient[0] = Material1->Ambient[1] = Material1->Ambient[2] = 0;
Material1->Transparent = 0;
Material1->Environment = NULL;
Material1->Texture = NULL; // Could do plReadTexturePCX() here for texture...
Material1->ShadeType = PL_SHADE_GOURAUD;
plMatInit(Material1);

//Material2->Priority = 1; // setup material 2
Material2->NumGradients = 100;
Material2->Diffuse[0] = 0;
Material2->Diffuse[1] = 0;
Material2->Diffuse[2] = 0;
Material2->Specular[0] = 160;
Material2->Specular[1] = 130;
Material2->Specular[2] = 0;
Material2->Shininess = 5;
Material2->Ambient[0] = Material2->Ambient[1] = Material2->Ambient[2] = 0;
Material2->Transparent = 0;
Material2->Environment = NULL;
Material2->Texture = NULL;
Material2->ShadeType = PL_SHADE_GOURAUD;
plMatInit(Material2);

AllMaterials[0] = Material1;
AllMaterials[1] = Material2;
plMatMakeOptPal(pal,1,255,AllMaterials,2); // Create a nice palette
pal[0] = pal[1] = pal[2] = 0; // Color 0 is black
plMatMapToPal(Material1,pal,0,255); // Map the material to our palette
plMatMapToPal(Material2,pal,0,255); // Map the material to our palette

exSetPalette(pal); // Set the palette
}

void SetUpColors() {
uint8_t pal[768]; // Our rgb triplet palette
pl_Mat *AllMaterials[2];
memset(pal,0,768);

//Material1->Priority = 0; // setup material 1
Material1->NumGradients = 200;
Material1->Diffuse[0] = 203;
Material1->Diffuse[1] = 212;
Material1->Diffuse[2] = 0;
Material1->Specular[0] = 128;
Material1->Specular[1] = 56;
Material1->Specular[2] = 0;
Material1->Shininess = 15;
Material1->Ambient[0] = Material1->Ambient[1] = Material1->Ambient[2] = 0;
Material1->Transparent = 0;
Material1->Environment = NULL;
Material1->Texture = NULL; // Could do plReadTexturePCX() here for texture...
Material1->ShadeType = PL_SHADE_GOURAUD;
plMatInit(Material1);

//Material2->Priority = 1; // setup material 2
Material2->NumGradients = 100;
Material2->Diffuse[0] = 0;
Material2->Diffuse[1] = 0;
Material2->Diffuse[2] = 0;
Material2->Specular[0] = 160;
Material2->Specular[1] = 130;
Material2->Specular[2] = 0;
Material2->Shininess = 5;
Material2->Ambient[0] = Material2->Ambient[1] = Material2->Ambient[2] = 0;
Material2->Transparent = 0;
Material2->Environment = NULL;
Material2->Texture = NULL;
Material2->ShadeType = PL_SHADE_GOURAUD;
plMatInit(Material2);

AllMaterials[0] = Material1;
AllMaterials[1] = Material2;
plMatMakeOptPal(pal,1,255,AllMaterials,2); // Create a nice palette
pal[0] = pal[1] = pal[2] = 0; // Color 0 is black
plMatMapToPal(Material1,pal,0,255); // Map the material to our palette
plMatMapToPal(Material2,pal,0,255); // Map the material to our palette

exSetPalette(pal); // Set the palette
int main(int argc, char **argv)
{
return exBegin(argc, argv, "DUCKDEMO: a Plush demo program.");
}
35 changes: 35 additions & 0 deletions examples/ex.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
// ex.h: provides a standard interface for video and keyboard for the
// example programs to use.

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include <float.h>
#include <time.h>
#include <math.h>

#include <plush/plush.h>

/* return codes from ex* functions */
enum {
PL_EXIT_CONTINUE = -1,
PL_EXIT_SUCCESS = 0,
PL_EXIT_FAILURE = 1
};

/* load your files and allocate your buffers here */
/* NOTE: will be called before a graphics context is ready */
/* return a value in *appstate and it will get passed in subsequent calls */
/* return the appropriate PL_EXIT_* code to exit or continue */
int exInit(void **appstate, int argc, char **argv);

/* called for each frame the program is running */
/* return the appropriate PL_EXIT_* code to exit or continue */
int exIterate(void *appstate);

/* called each time the user presses a key */
/* return the appropriate PL_EXIT_* code to exit or continue */
int exKeyEvent(void *appstate, int key);

/* called when the program is shutting down */
/* code will be whatever PL_EXIT_* value caused the program to exit */
void exQuit(void *appstate, int code);

#if defined(PLUSH_EXAMPLE_SDL2)
#include "ex_sdl2.h"
#elif defined(PLUSH_EXAMPLE_SDL3)
Expand Down
Loading