Browse Source

Created three application templates.

David Piuva 5 years ago
parent
commit
7414ae6ebe

+ 8 - 0
Source/templates/Readme.txt

@@ -0,0 +1,8 @@
+To get started with your own project:
+* Copy one of these folders into a location with stable relative access to the DFPSR library.
+  You can also place DFPSR in a standard path and refer to it using absolute paths.
+* Change #include "../../DFPSR/includeFramework.h" in main.cpp to find the header from the new location.
+* Change ROOT_PATH=../.. the same way in build.sh or just delete the whole build script if you're not going to use it.
+* Give execution permission using chmod +x build.sh. (if using Linux)
+* Build and run your new application. (./build.sh if using Linux)
+

+ 26 - 0
Source/templates/basic3D/build.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Assuming that you called build.sh from its own folder, you should already be in the project folder.
+PROJECT_FOLDER=.
+# Placing your executable in the project folder allow using the same relative paths in the final release.
+TARGET_FILE=./application
+# The root folder is where DFPSR, SDK and tools are located.
+ROOT_PATH=../..
+# Select where to place temporary files and the generated executable
+TEMP_DIR=${ROOT_PATH}/../../temporary
+# Select a window manager
+WINDOW_MANAGER=X11
+# Select safe debug mode or fast release mode
+MODE=-DDEBUG #Debug mode
+#MODE=-DNDEBUG #Release mode
+# Select the version of C++
+CPP_VERSION=-std=c++14
+# Select optimization level
+O_LEVEL=-O2
+# Select external libraries
+LINKER_FLAGS=""
+
+# Give execution permission
+chmod +x ${ROOT_PATH}/tools/buildAndRun.sh;
+# Compile everything
+${ROOT_PATH}/tools/buildAndRun.sh "${PROJECT_FOLDER}" "${TARGET_FILE}" "${ROOT_PATH}" "${TEMP_DIR}" "${WINDOW_MANAGER}" "${MODE}" "${CPP_VERSION}" "${O_LEVEL}" "${LINKER_FLAGS}";

+ 133 - 0
Source/templates/basic3D/main.cpp

@@ -0,0 +1,133 @@
+
+#include <limits>
+#include "../../DFPSR/includeFramework.h"
+
+using namespace dsr;
+
+// Having a media folder requires keeping track of its location.
+// * You can start the program from its own folder and use a relative path. (Simple and works when developing)
+// * You can give a command line argument with the media folder's location. (Good for modifications using multiple folders)
+// * You can try to find the application's relative path using argv[0]. (Often works but not guaranteed by the C++ standard)
+// * You can use system specific API calls to find the applications location. (Increases long-term maintenance)
+// * You can link binary files into your application. (Hard to create modifications without access to source code)
+
+//const String mediaPath = string_combine(U"media", file_separator());
+//static BasicResourcePool pool(mediaPath);
+
+// Global variables
+bool running = true;
+
+// The window handle
+Window window;
+
+// Textures for 3D models must use power-of-two dimensions
+AlignedImageU8 darkEdge = image_fromAscii(
+	"< .-x>"
+	"<xxxxxxxxxxxxxxxx>"
+	"<x--------------x>"
+	"<x-............-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-.          .-x>"
+	"<x-............-x>"
+	"<x--------------x>"
+	"<xxxxxxxxxxxxxxxx>"
+);
+OrderedImageRgbaU8 myTexture = image_pack(darkEdge, darkEdge, 0, 255);
+
+int createCubePart(Model model, const FVector3D &min, const FVector3D &max) {
+	// Add positions
+	model_addPoint(model, FVector3D(min.x, min.y, min.z)); // 0: Left-down-near
+	model_addPoint(model, FVector3D(min.x, min.y, max.z)); // 1: Left-down-far
+	model_addPoint(model, FVector3D(min.x, max.y, min.z)); // 2: Left-up-near
+	model_addPoint(model, FVector3D(min.x, max.y, max.z)); // 3: Left-up-far
+	model_addPoint(model, FVector3D(max.x, min.y, min.z)); // 4: Right-down-near
+	model_addPoint(model, FVector3D(max.x, min.y, max.z)); // 5: Right-down-far
+	model_addPoint(model, FVector3D(max.x, max.y, min.z)); // 6: Right-up-near
+	model_addPoint(model, FVector3D(max.x, max.y, max.z)); // 7: Right-up-far
+	// Create a part for the polygons
+	int part = model_addEmptyPart(model, U"cube");
+	// Polygons using default texture coordinates on the 4 corners of the texture
+	model_addQuad(model, part, 3, 2, 0, 1); // Left quad
+	model_addQuad(model, part, 6, 7, 5, 4); // Right quad
+	model_addQuad(model, part, 2, 6, 4, 0); // Front quad
+	model_addQuad(model, part, 7, 3, 1, 5); // Back quad
+	model_addQuad(model, part, 3, 7, 6, 2); // Top quad
+	model_addQuad(model, part, 0, 4, 5, 1); // Bottom quad
+	return part;
+}
+
+Model createCubeModel(const FVector3D &min, const FVector3D &max) {
+	Model result = model_create();
+	createCubePart(result, min, max);
+	return result;
+}
+
+int main(int argn, char **argv) {
+	// Create a window
+	window = window_create(U"Basic 3D template", 1600, 900);
+
+	// Tell the application to terminate when the window is closed
+	window_setCloseEvent(window, []() {
+		running = false;
+	});
+
+	// Get whole window key events
+	window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
+		if (event.keyboardEventType == KeyboardEventType::KeyDown) {
+			DsrKey key = event.dsrKey;
+			if (key >= DsrKey_1 && key <= DsrKey_9) {
+				window_setPixelScale(window, key - DsrKey_0);
+			} else if (key == DsrKey_F11) {
+				window_setFullScreen(window, !window_isFullScreen(window));
+			} else if (key == DsrKey_Escape) {
+				running = false;
+			}
+		}
+	});
+
+	// Genrate mip-maps for the texture
+	image_generatePyramid(myTexture);
+	// Create a cube model
+	Model cubeModel = createCubeModel(FVector3D(-0.5f), FVector3D(0.5f));
+	// Assign the texture to part 0
+	model_setDiffuseMap(cubeModel, 0, myTexture);
+
+	// Create a renderer for multi-threading
+	Renderer worker = renderer_create();
+
+	while(running) {
+		window_executeEvents(window);
+		auto colorBuffer = window_getCanvas(window);
+		auto depthBuffer = window_getDepthBuffer(window);
+		int targetWidth = image_getWidth(colorBuffer);
+		int targetHeight = image_getHeight(colorBuffer);
+
+		// Paint the background color
+		image_fill(colorBuffer, ColorRgbaI32(0, 0, 0, 0));
+		image_fill(depthBuffer, 0.0f); // Infinite reciprocal depth using zero
+
+		// Create a camera
+		const float distance = 1.3f;
+		const float height = 1.0f;
+		const double speed = 0.2f;
+		double timer = time_getSeconds() * speed;
+		FVector3D cameraPosition = FVector3D(sin(timer) * distance, height, cos(timer) * distance);
+		FMatrix3x3 cameraRotation = FMatrix3x3::makeAxisSystem(-cameraPosition, FVector3D(0.0f, 1.0f, 0.0f));
+		Camera camera = Camera::createPerspective(Transform3D(cameraPosition, cameraRotation), targetWidth, targetHeight);
+
+		// Render
+		renderer_begin(worker, colorBuffer, depthBuffer);
+		renderer_giveTask(worker, cubeModel, Transform3D(), camera);
+		renderer_end(worker);
+
+		window_showCanvas(window);
+	}
+}

+ 26 - 0
Source/templates/basicCLI/build.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Assuming that you called build.sh from its own folder, you should already be in the project folder.
+PROJECT_FOLDER=.
+# Placing your executable in the project folder allow using the same relative paths in the final release.
+TARGET_FILE=./application
+# The root folder is where DFPSR, SDK and tools are located.
+ROOT_PATH=../..
+# Select where to place temporary files and the generated executable
+TEMP_DIR=${ROOT_PATH}/../../temporary
+# Select a window manager
+WINDOW_MANAGER=NONE
+# Select safe debug mode or fast release mode
+MODE=-DDEBUG #Debug mode
+#MODE=-DNDEBUG #Release mode
+# Select the version of C++
+CPP_VERSION=-std=c++14
+# Select optimization level
+O_LEVEL=-O2
+# Select external libraries
+LINKER_FLAGS=""
+
+# Give execution permission
+chmod +x ${ROOT_PATH}/tools/buildAndRun.sh;
+# Compile everything
+${ROOT_PATH}/tools/buildAndRun.sh "${PROJECT_FOLDER}" "${TARGET_FILE}" "${ROOT_PATH}" "${TEMP_DIR}" "${WINDOW_MANAGER}" "${MODE}" "${CPP_VERSION}" "${O_LEVEL}" "${LINKER_FLAGS}";

+ 8 - 0
Source/templates/basicCLI/main.cpp

@@ -0,0 +1,8 @@
+
+#include "../../DFPSR/includeFramework.h"
+
+using namespace dsr;
+
+int main(int argn, char **argv) {
+	printText(U"Hello world\n");
+}

+ 26 - 0
Source/templates/basicGUI/build.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Assuming that you called build.sh from its own folder, you should already be in the project folder.
+PROJECT_FOLDER=.
+# Placing your executable in the project folder allow using the same relative paths in the final release.
+TARGET_FILE=./application
+# The root folder is where DFPSR, SDK and tools are located.
+ROOT_PATH=../..
+# Select where to place temporary files and the generated executable
+TEMP_DIR=${ROOT_PATH}/../../temporary
+# Select a window manager
+WINDOW_MANAGER=X11
+# Select safe debug mode or fast release mode
+MODE=-DDEBUG #Debug mode
+#MODE=-DNDEBUG #Release mode
+# Select the version of C++
+CPP_VERSION=-std=c++14
+# Select optimization level
+O_LEVEL=-O2
+# Select external libraries
+LINKER_FLAGS=""
+
+# Give execution permission
+chmod +x ${ROOT_PATH}/tools/buildAndRun.sh;
+# Compile everything
+${ROOT_PATH}/tools/buildAndRun.sh "${PROJECT_FOLDER}" "${TARGET_FILE}" "${ROOT_PATH}" "${TEMP_DIR}" "${WINDOW_MANAGER}" "${MODE}" "${CPP_VERSION}" "${O_LEVEL}" "${LINKER_FLAGS}";

+ 56 - 0
Source/templates/basicGUI/main.cpp

@@ -0,0 +1,56 @@
+
+#include "../../DFPSR/includeFramework.h"
+
+using namespace dsr;
+
+// Embedding your interface's layout is the simplest way to get started
+// It works even if the application is called from another folder
+String interfaceContent =
+UR"QUOTE(
+Begin : Panel
+	Name = "mainPanel"
+	Color = 150,160,170
+	Solid = 1
+End
+)QUOTE";
+
+// Global
+bool running = true;
+
+// GUI handles
+Window window;
+
+int main(int argn, char **argv) {
+	// Create a window
+	window = window_create(U"GUI template", 1000, 700);
+
+	// Register your custom components here
+	//REGISTER_PERSISTENT_CLASS(className);
+
+	// Load an interface to the window
+	window_loadInterfaceFromString(window, interfaceContent);
+
+	// Bind methods to events
+	window_setCloseEvent(window, []() {
+		running = false;
+	});
+
+	// Get your component handles here
+	//myComponent = window_findComponentByName(window, U"myComponent");
+
+	// Bind your components to events here
+	//component_setPressedEvent(myButton, []() {});
+
+	// Execute
+	while(running) {
+		// Wait for actions so that we don't render until an action has been recieved
+		// This will save battery on laptops for applications that don't require animation
+		while (!window_executeEvents(window)) {
+			time_sleepSeconds(0.01);
+		}
+		// Draw interface
+		window_drawComponents(window);
+		// Show the final image
+		window_showCanvas(window);
+	}
+}