Im using dbPickObject() to pick an object on the screen and then you can move or rotate the object using the mouse. Right now I am just using the generic shapes like cube, sphere, etc. The code is a little ugly but commented. Anyway the object seems to jump whenever I am trying to position it. What you will see is that the object will jump around when you left click on it. And if you right click, it will jump in rotation. So not sure if this actually my fault or a bug in the SDK. Only thing I can think of if its my fault is timing. But putting dbWait() doesnt seem to help much unless I put a large value, and that decreases FPS a lot. The controls are:
POSITIONING
- click object and hold left mouse button and move mouse, selects object and moves object along x and z axis.
- click object and hold left mouse button and move scroll button, selects object and moves object along y axis.
ROTATING
- click object and hold right mouse button and move mouse, selects object and rotates object.
- click object and hold right mouse button and move scroll button, selects object and rotates object differently.
MOVING CAMERA
- click up arrow to move forward and click down arrow to move backward.
- hold CTRL and move the mouse, move the camera like in a FPS.
Also this code is for a console win32 program, delete main() if you want to use a regular win32 program.
editor.cpp
/*********************** INCLUDES ************************/
#include "Editor.h"
/****************** GLOBAL VARIABLES ********************/
Object object;
bool objectSelected = false;
int pickedObject = 0;
//used for camera control
float g_fSpeed = 0.1f;
float g_fTurn = 0.03f;
float OldCamAngleY;
float OldCamAngleX;
float CameraAngleY;
float CameraAngleX;
/****************** GLOBAL FUNCTIONS ********************/
//creates a cone
void initCone()
{
object.ID = 1;
object.size = 5;
object.positionX = 0;
object.positionY = 0;
object.positionZ = 0;
dbMakeObjectCone(object.ID, object.size);
dbPositionObject(object.ID, object.positionX, object.positionY, object.positionZ);
dbShowObjectBounds(object.ID, 1);
}
//creates a cube
void initCube()
{
object.ID = 1;
object.size = 5;
object.positionX = 0;
object.positionY = 0;
object.positionZ = 0;
dbMakeObjectCube(object.ID, object.size);
dbPositionObject(object.ID, object.positionX, object.positionY, object.positionZ);
dbShowObjectBounds(object.ID, 1);
}
//creates a cylinder
void initCylinder()
{
object.ID = 1;
object.size = 5;
object.positionX = 0;
object.positionY = 0;
object.positionZ = 0;
dbMakeObjectCylinder(object.ID, object.size);
dbPositionObject(object.ID, object.positionX, object.positionY, object.positionZ);
dbShowObjectBounds(object.ID, 1);
}
//creates a sphere
void initSphere()
{
object.ID = 1;
object.size = 5;
object.positionX = 0;
object.positionY = 0;
object.positionZ = 0;
dbMakeObjectSphere(object.ID, object.size);
dbPositionObject(object.ID, object.positionX, object.positionY, object.positionZ);
dbShowObjectBounds(object.ID, 1);
}
//processes all mouse input
void processMouseInput()
{
switch(dbMouseClick())
{
case MOUSE_LEFT_BUTTON:
//selects an object on the screen with a left click
if(!objectSelected)
{
pickedObject = dbPickObject(dbMouseX(), dbMouseY(), 1, 5);
if(pickedObject != 0) objectSelected = true;
}
//positions selected object when holding left mouse button and moving mouse
//can move along the x and z axis by moving the mouse and move along y axis
//using the scroll button
if(pickedObject != 0)
{
object.positionX += (float)(dbMouseMoveX() * 0.1);
object.positionY += (float)(dbMouseMoveZ() * 0.01);
object.positionZ += (float)(dbMouseMoveY() * -0.1);
dbPositionObject(pickedObject, object.positionX, object.positionY, object.positionZ);
}
break;
case MOUSE_RIGHT_BUTTON:
//selects an object on the screen with a right click
if(!objectSelected)
{
pickedObject = dbPickObject(dbMouseX(), dbMouseY(), 1, 5);
if(pickedObject != 0) objectSelected = true;
}
//rotates selected object when holding right mouse button and moving mouse
if(pickedObject != 0)
{
dbTurnObjectLeft(pickedObject, (float)(dbMouseMoveX() * 0.2));
dbPitchObjectUp(pickedObject, (float)(dbMouseMoveY() * 0.2));
dbRollObjectLeft(pickedObject, (float)(dbMouseMoveZ() * 0.1));
}
break;
case MOUSE_MIDDLE_BUTTON:
break;
default:
//just used to clear any selected objects when mouse buttons not depressed
objectSelected = false;
break;
}
}
//handles all keyboard and mouse input
void userInput(void)
{
//controls the camera using arrow keys
dbControlCameraUsingArrowKeys(0, g_fSpeed, g_fTurn);
//update current mouse position
processMouseInput();
switch(dbScanCode())
{
case CODE_CONTROL_BUTTON:
//lets you move the camera around like if you were playing a FPS
OldCamAngleY = CameraAngleY;
OldCamAngleX = CameraAngleX;
CameraAngleY = dbWrapValue(CameraAngleY + dbMouseMoveX() * 0.4f);
CameraAngleX = dbWrapValue(CameraAngleX + dbMouseMoveY() * 0.4f);
dbYRotateCamera(dbCurveAngle(CameraAngleY, OldCamAngleY, 24));
dbXRotateCamera(dbCurveAngle(CameraAngleX, OldCamAngleX, 24));
break;
}
//just for debugging
int keyPressed = dbScanCode();
if(keyPressed != 0) printf("key = %dn", keyPressed);
}
//MUST HAVE THIS FUNCTION
//This is the DGSDK game loop.
void DarkSDK(void)
{
char szFPS[50] = "";
//must have for DGSDK
dbSyncOn();
//creates an object
//initCone();
initCube();
//initCylinder();
//initSphere();
//must have for DGSDK
while(LoopSDK())
{
//exits program if ESC pressed
if(dbEscapeKey()) return;
//handles all user input
userInput();
//prints FPS to screen
sprintf(szFPS, "fps = %d", dbScreenFPS());
dbText(dbScreenWidth() - 20 - dbTextWidth(szFPS), dbScreenHeight() - 40, szFPS);
//must have for DGSDK
dbSync();
}
//deletes the cube
dbDeleteObject(object.ID);
}
//DO NOT MODIFY
//a hack to call winmain (inside the DarkSDK lib) from a main()
//this allows you to create a console win32 program
extern int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdLine, int cmdShow);
int main(int argc, char **argv)
{
std::string commandLine = "";
if(argc > 1)
{
commandLine = argv[1];
for(int i = 2; i < argc; i++)
{
commandLine += " ";
commandLine += argv[i];
}
}
return WinMain((HINSTANCE)(GetModuleHandle(0)), 0, (LPSTR)commandLine.c_str(), SW_SHOW);
}
editor.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <vector>
#include <DarkSDK.h>
#define TRUE 1
#define FALSE 0
//keyboard input (scancodes)
#define CODE_CONTROL_BUTTON 157
//mouse input
#define MOUSE_LEFT_BUTTON 1
#define MOUSE_RIGHT_BUTTON 2
#define MOUSE_MIDDLE_BUTTON 4
#define MOUSE_MISC_BUTTON 8
struct Object
{
int ID;
float size;
float positionX;
float positionY;
float positionZ;
};