Skip to content

Commit

Permalink
Add 3D
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Aug 10, 2019
1 parent 14acffe commit 9bb55ed
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 40 deletions.
93 changes: 93 additions & 0 deletions examples/core/core_3d_camera_first_person.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*******************************************************************************************
*
* raylib [core] example - 3d camera first person
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib/raylib.h"

#define MAX_COLUMNS 20

int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

raylib::Window w(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
raylib::Color background(RAYWHITE);

// Define the camera to look into our 3d world (position, target, up vector)
raylib::Camera camera();
camera.position = raylib::Vector3(4.0f, 2.0f, 4.0f);
camera.target = raylib::Vector3(0.0f, 1.8f, 0.0f);
camera.up = raylib::Vector3(0.0f, 1.0f, 0.0f);
camera.fovy = 60.0f;
camera.type = CAMERA_PERSPECTIVE;

// Generates some random columns
float heights[MAX_COLUMNS] = { 0.0f };
raylib::Vector3 positions[MAX_COLUMNS] = { 0 };
raylib::Color colors[MAX_COLUMNS] = { 0 };

for (int i = 0; i < MAX_COLUMNS; i++)
{
heights[i] = (float)GetRandomValue(1, 12);
positions[i] = raylib::Vector3(GetRandomValue(-15, 15), heights[i]/2, GetRandomValue(-15, 15));
colors[i] = raylib::Color(GetRandomValue(20, 255), GetRandomValue(10, 55), 30);
}

camera.SetMode(CAMERA_FIRST_PERSON); // Set a first person camera mode

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
while (!w.ShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
camera.Update(); // Update camera
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

background.ClearBackground();

camera.BeginMode3D();

DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall

// Draw some cubes around
for (int i = 0; i < MAX_COLUMNS; i++)
{
positions[i].DrawCube(2.0f, heights[i], 2.0f, colors[i]);
positions[i].DrawCubeWires(2.0f, heights[i], 2.0f, MAROON);
}

EndMode3D();

DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 220, 70, BLUE);

DrawText("First person camera default controls:", 20, 20, 10, BLACK);
DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY);
DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY);

EndDrawing();
//----------------------------------------------------------------------------------
}

return 0;
}
36 changes: 19 additions & 17 deletions examples/text/text_raylib_fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int main()
int screenHeight = 450;

raylib::Window w(screenWidth, screenHeight, "raylib [text] example - raylib fonts");
raylib::Color background(RAYWHITE);
raylib::Color textColor(DARKGRAY);

// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
raylib::Font fonts[MAX_FONTS] = {
Expand All @@ -39,31 +41,31 @@ int main()
raylib::Font("resources/fonts/jupiter_crash.png")
};

std::string messages[MAX_FONTS] = { "ALAGARD FONT designed by Hewett Tsoi",
std::string messages[MAX_FONTS] = { "ALAGARD FONT designed by Hewett Tsoi",
"PIXELPLAY FONT designed by Aleksander Shevchuk",
"MECHA FONT designed by Captain Falcon",
"SETBACK FONT designed by Brian Kent (AEnigma)",
"ROMULUS FONT designed by Hewett Tsoi",
"MECHA FONT designed by Captain Falcon",
"SETBACK FONT designed by Brian Kent (AEnigma)",
"ROMULUS FONT designed by Hewett Tsoi",
"PIXANTIQUA FONT designed by Gerhard Grossmann",
"ALPHA_BETA FONT designed by Brian Kent (AEnigma)",
"JUPITER_CRASH FONT designed by Brian Kent (AEnigma)" };

const int spacings[MAX_FONTS] = { 2, 4, 8, 4, 3, 4, 4, 1 };

Vector2 positions[MAX_FONTS];

for (int i = 0; i < MAX_FONTS; i++)
{
positions[i].x = screenWidth/2 - MeasureTextEx(fonts[i], messages[i].c_str(), fonts[i].baseSize*2, spacings[i]).x/2;
positions[i].y = 60 + fonts[i].baseSize + 45*i;
}

// Small Y position corrections
positions[3].y += 8;
positions[4].y += 2;
positions[7].y -= 8;
Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };

raylib::Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
//--------------------------------------------------------------------------------------

// Main game loop
Expand All @@ -78,14 +80,14 @@ int main()
//----------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);
DrawText("free fonts included with raylib", 250, 20, 20, DARKGRAY);
DrawLine(220, 50, 590, 50, DARKGRAY);
background.ClearBackground();

textColor.DrawText("free fonts included with raylib", 250, 20, 20);
textColor.DrawLine(220, 50, 590, 50);

for (int i = 0; i < MAX_FONTS; i++)
{
DrawTextEx(fonts[i], messages[i].c_str(), positions[i], fonts[i].baseSize*2, spacings[i], colors[i]);
fonts[i].DrawText(messages[i].c_str(), positions[i], fonts[i].baseSize*2, spacings[i], colors[i]);
}

EndDrawing();
Expand All @@ -94,4 +96,4 @@ int main()
//--------------------------------------------------------------------------------------

return 0;
}
}
8 changes: 5 additions & 3 deletions include/raylib/Camera3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace raylib {
type = typeValue;
};

Camera3D() {}

inline void set(const ::Camera3D& camera) {
position = camera.position;
target = camera.target;
Expand All @@ -39,15 +41,15 @@ namespace raylib {
::EndMode3D();
}

Matrix GetCameraMatrix() {
Matrix GetMatrix() {
return ::GetCameraMatrix(*this);
}

void SetCameraMode(int mode) {
void SetMode(int mode) {
::SetCameraMode(*this, mode);
}

void UpdateCamera() {
void Update() {
::UpdateCamera(this);
}

Expand Down
57 changes: 37 additions & 20 deletions include/raylib/Color.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RAYLIB_CPP_COLOR_HPP_
#define RAYLIB_CPP_COLOR_HPP_

#include <string>

#include "raylib.h"
#include "Vector4.hpp"
#include "utils.hpp"
Expand Down Expand Up @@ -72,31 +74,46 @@ namespace raylib {
GETTERSETTER(unsigned char,B,b)
GETTERSETTER(unsigned char,A,a)

Color& operator=(const ::Color& color) {
set(color);
return *this;
}
Color& operator=(const ::Color& color) {
set(color);
return *this;
}

Color& operator=(const Color& color) {
set(color);
return *this;
}

void ClearBackground() {
::ClearBackground(*this);
}

void DrawPixel(int x, int y) {
::DrawPixel(x, y, *this);
}

Color& operator=(const Color& color) {
set(color);
return *this;
}
void DrawPixel(::Vector2 pos) {
::DrawPixelV(pos, *this);
}

void ClearBackground() {
::ClearBackground(*this);
}
void DrawText(const std::string& text, int posX, int posY, int fontSize) {
::DrawText(text.c_str(), posX, posY, fontSize, *this);
}
void DrawText(::Font font, const std::string& text, ::Vector2 position, float fontSize, float spacing) {
::DrawTextEx(font, text.c_str(), position, fontSize, spacing, *this);
}

void DrawPixel(int x, int y) {
::DrawPixel(x, y, *this);
}
void DrawText(::Font font, const std::string& text, ::Rectangle rec, float fontSize, float spacing, bool wordWrap = false) {
::DrawTextRec(font, text.c_str(), rec, fontSize, spacing, wordWrap, *this);
}

void DrawPixel(::Vector2 pos) {
::DrawPixelV(pos, *this);
}
void DrawRectangleLines(int posX, int posY, int width, int height) {
::DrawRectangleLines(posX, posY, width, height, *this);
}

void DrawText(const std::string& text, int posX, int posY, int fontSize) {
::DrawText(text.c_str(), posX, posY, fontSize, *this);
}
void DrawRectangleLines(Rectangle rec, int lineThick) {
::DrawRectangleLinesEx(rec, lineThick, *this);
}

};
}
Expand Down
27 changes: 27 additions & 0 deletions include/raylib/Vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ namespace raylib {
void DrawGizmo() {
::DrawGizmo(*this);
}

void DrawLine3D(::Vector3 endPos, ::Color color) {
::DrawLine3D(*this, endPos, color);
}

void DrawCube(float width, float height, float length, ::Color color) {
::DrawCube(*this, width, height, length, color);
}
void DrawCube(::Vector3 size, ::Color color) {
::DrawCubeV(*this, size, color);
}


void DrawCubeWires(float width, float height, float length, ::Color color) {
::DrawCubeWires(*this, width, height, length, color);
}
void DrawCubeWires(::Vector3 size, ::Color color) {
::DrawCubeWiresV(*this, size, color);
}

void DrawCubeTexture(::Texture2D texture, float width, float height, float length, ::Color color) {
::DrawCubeTexture(texture, *this, width, height, length, color);
}

void DrawPlane(::Vector2 size, ::Color color) {
::DrawPlane(*this, size, color);
}
};
}

Expand Down

0 comments on commit 9bb55ed

Please sign in to comment.