Skip to content

Commit

Permalink
Section: BONUS! Interactive Light
Browse files Browse the repository at this point in the history
  • Loading branch information
enobayram committed Nov 14, 2015
1 parent 64921d0 commit c42f8d4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Binary file added light_bulb.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 41 additions & 1 deletion text_and_shaders.cpp
@@ -1,14 +1,54 @@
#include <orx.h>

orxOBJECT * light_bulb;

// Now we need to keep the viewport in a global variable
orxVIEWPORT * viewport;

// This method will be called whenever Orx needs the parameters for our shader
orxSTATUS orxFASTCALL lightPosHandler(const orxEVENT *_pstEvent) {
if(_pstEvent->eID == orxSHADER_EVENT_SET_PARAM) {
orxSHADER_EVENT_PAYLOAD * payload = (orxSHADER_EVENT_PAYLOAD *)_pstEvent->pstPayload;
if(strcmp(payload->zParamName, "LightPos") == 0) {
// Here we set the "LightPos" parameter to be the mouse position
orxMouse_GetPosition(&(payload->vValue));
payload->vValue.fZ = 100;
}
}
return orxSTATUS_SUCCESS;
}

orxSTATUS orxFASTCALL Init()
{
orxViewport_CreateFromConfig("Viewport");
viewport = orxViewport_CreateFromConfig("Viewport");
orxObject_CreateFromConfig("TextObject");

// Create the light bulb
light_bulb = orxObject_CreateFromConfig("LightBulb");

// Register our shader parameter handler
orxEvent_AddHandler ( orxEVENT_TYPE_SHADER, lightPosHandler);
return orxSTATUS_SUCCESS;
}

orxSTATUS orxFASTCALL Run()
{
orxVECTOR mousePos;
orxVECTOR mouseInWorld;

// Get the mouse position on screen
orxMouse_GetPosition(&mousePos);

// Get the mouse position in the game world,
// note that the mouse might not be in our game's window
orxVECTOR * inWindow = orxRender_GetWorldPosition(&mousePos, viewport, &mouseInWorld);
if(inWindow) {
mouseInWorld.fZ = -0.1;
} else {
mouseInWorld.fZ = -2; // Out of camera frustum, so the light_bulb is hidden
}
orxObject_SetPosition(light_bulb, &mouseInWorld);

return orxSTATUS_SUCCESS;
}

Expand Down
10 changes: 9 additions & 1 deletion text_and_shaders.ini
Expand Up @@ -55,6 +55,14 @@ void main() {
}"
BorderColor = (0,0,255) ; Let's make the border blue, just for fun.
LightPos = (200,150,100) ;
LightColor = (1,0.7,0.7) ;
LightColor = (1,1,0.18) ;
UseCustomParam = true ; Since the LightPos parameter will change at runtime we need this.

[LightBulb]
Graphic = @
Texture = light_bulb.png
Pivot = (64,53,0)
Scale = 0.5
Position = (0,0,-0.1)

@plump.ini@

0 comments on commit c42f8d4

Please sign in to comment.