Parcourir la source

Added DrawTexturePro

quantumde1 il y a 9 mois
Parent
commit
1bc45a3e3f
5 fichiers modifiés avec 243 ajouts et 32 suppressions
  1. 153 15
      bindings/dlang/source/aperture.d
  2. 13 1
      include/base.h
  3. 2 1
      include/textures.h
  4. 25 0
      source/gl/base.c
  5. 50 15
      source/gl/textures.c

+ 153 - 15
bindings/dlang/source/aperture.d

@@ -23,20 +23,158 @@ enum Colors {
     BLUE = Color(0, 0, 255, 255)
 }
 
+enum KeyboardKey
+{
+    KEY_NULL = 0, // Key: NULL, used for no key pressed
+    // Alphanumeric keys
+    KEY_APOSTROPHE = 39, // Key: '
+    KEY_COMMA = 44, // Key: ,
+    KEY_MINUS = 45, // Key: -
+    KEY_PERIOD = 46, // Key: .
+    KEY_SLASH = 47, // Key: /
+    KEY_ZERO = 48, // Key: 0
+    KEY_ONE = 49, // Key: 1
+    KEY_TWO = 50, // Key: 2
+    KEY_THREE = 51, // Key: 3
+    KEY_FOUR = 52, // Key: 4
+    KEY_FIVE = 53, // Key: 5
+    KEY_SIX = 54, // Key: 6
+    KEY_SEVEN = 55, // Key: 7
+    KEY_EIGHT = 56, // Key: 8
+    KEY_NINE = 57, // Key: 9
+    KEY_SEMICOLON = 59, // Key: ;
+    KEY_EQUAL = 61, // Key: =
+    KEY_A = 65, // Key: A | a
+    KEY_B = 66, // Key: B | b
+    KEY_C = 67, // Key: C | c
+    KEY_D = 68, // Key: D | d
+    KEY_E = 69, // Key: E | e
+    KEY_F = 70, // Key: F | f
+    KEY_G = 71, // Key: G | g
+    KEY_H = 72, // Key: H | h
+    KEY_I = 73, // Key: I | i
+    KEY_J = 74, // Key: J | j
+    KEY_K = 75, // Key: K | k
+    KEY_L = 76, // Key: L | l
+    KEY_M = 77, // Key: M | m
+    KEY_N = 78, // Key: N | n
+    KEY_O = 79, // Key: O | o
+    KEY_P = 80, // Key: P | p
+    KEY_Q = 81, // Key: Q | q
+    KEY_R = 82, // Key: R | r
+    KEY_S = 83, // Key: S | s
+    KEY_T = 84, // Key: T | t
+    KEY_U = 85, // Key: U | u
+    KEY_V = 86, // Key: V | v
+    KEY_W = 87, // Key: W | w
+    KEY_X = 88, // Key: X | x
+    KEY_Y = 89, // Key: Y | y
+    KEY_Z = 90, // Key: Z | z
+    KEY_LEFT_BRACKET = 91, // Key: [
+    KEY_BACKSLASH = 92, // Key: '\'
+    KEY_RIGHT_BRACKET = 93, // Key: ]
+    KEY_GRAVE = 96, // Key: `
+    // Function keys
+    KEY_SPACE = 32, // Key: Space
+    KEY_ESCAPE = 256, // Key: Esc
+    KEY_ENTER = 257, // Key: Enter
+    KEY_TAB = 258, // Key: Tab
+    KEY_BACKSPACE = 259, // Key: Backspace
+    KEY_INSERT = 260, // Key: Ins
+    KEY_DELETE = 261, // Key: Del
+    KEY_RIGHT = 262, // Key: Cursor right
+    KEY_LEFT = 263, // Key: Cursor left
+    KEY_DOWN = 264, // Key: Cursor down
+    KEY_UP = 265, // Key: Cursor up
+    KEY_PAGE_UP = 266, // Key: Page up
+    KEY_PAGE_DOWN = 267, // Key: Page down
+    KEY_HOME = 268, // Key: Home
+    KEY_END = 269, // Key: End
+    KEY_CAPS_LOCK = 280, // Key: Caps lock
+    KEY_SCROLL_LOCK = 281, // Key: Scroll down
+    KEY_NUM_LOCK = 282, // Key: Num lock
+    KEY_PRINT_SCREEN = 283, // Key: Print screen
+    KEY_PAUSE = 284, // Key: Pause
+    KEY_F1 = 290, // Key: F1
+    KEY_F2 = 291, // Key: F2
+    KEY_F3 = 292, // Key: F3
+    KEY_F4 = 293, // Key: F4
+    KEY_F5 = 294, // Key: F5
+    KEY_F6 = 295, // Key: F6
+    KEY_F7 = 296, // Key: F7
+    KEY_F8 = 297, // Key: F8
+    KEY_F9 = 298, // Key: F9
+    KEY_F10 = 299, // Key: F10
+    KEY_F11 = 300, // Key: F11
+    KEY_F12 = 301, // Key: F12
+    KEY_LEFT_SHIFT = 340, // Key: Shift left
+    KEY_LEFT_CONTROL = 341, // Key: Control left
+    KEY_LEFT_ALT = 342, // Key: Alt left
+    KEY_LEFT_SUPER = 343, // Key: Super left
+    KEY_RIGHT_SHIFT = 344, // Key: Shift right
+    KEY_RIGHT_CONTROL = 345, // Key: Control right
+    KEY_RIGHT_ALT = 346, // Key: Alt right
+    KEY_RIGHT_SUPER = 347, // Key: Super right
+    KEY_KB_MENU = 348, // Key: KB menu
+    // Keypad keys
+    KEY_KP_0 = 320, // Key: Keypad 0
+    KEY_KP_1 = 321, // Key: Keypad 1
+    KEY_KP_2 = 322, // Key: Keypad 2
+    KEY_KP_3 = 323, // Key: Keypad 3
+    KEY_KP_4 = 324, // Key: Keypad 4
+    KEY_KP_5 = 325, // Key: Keypad 5
+    KEY_KP_6 = 326, // Key: Keypad 6
+    KEY_KP_7 = 327, // Key: Keypad 7
+    KEY_KP_8 = 328, // Key: Keypad 8
+    KEY_KP_9 = 329, // Key: Keypad 9
+    KEY_KP_DECIMAL = 330, // Key: Keypad .
+    KEY_KP_DIVIDE = 331, // Key: Keypad /
+    KEY_KP_MULTIPLY = 332, // Key: Keypad *
+    KEY_KP_SUBTRACT = 333, // Key: Keypad -
+    KEY_KP_ADD = 334, // Key: Keypad +
+    KEY_KP_ENTER = 335, // Key: Keypad Enter
+    KEY_KP_EQUAL = 336, // Key: Keypad =
+    // Android key buttons
+    KEY_BACK = 4, // Key: Android back button
+    KEY_MENU = 5, // Key: Android menu button
+    KEY_VOLUME_UP = 24, // Key: Android volume up button
+    KEY_VOLUME_DOWN = 25 // Key: Android volume down button
+}
+
+struct Vector2 {
+    float x;
+    float y;
+}
+
+struct Rectangle {
+    float x;
+    float y;
+    float width;
+    float height;
+}
+
 extern (C) @nogc {
-    void SetTargetFPS(int fps);
-    void DrawText(const char* text, float x, float y, float scale, Color color = Color(255, 255, 255, 255));
-    float GetScreenWidth();
-    float GetScreenHeight();
-    void BeginDrawing();
-    void EndDrawing();
-    void ClearBackground(Color color);
-    void InitWindow(int argc, char** argv, int width, int height, const char* title);
-    void DrawRectangle(float x, float y, float width, float height, Color color = Color(255, 255, 255, 255));
-    Texture LoadTexture(const char* filename);
-    void DrawTexture(Texture texture, float x, float y, float scale, Color tint = Color(255, 255, 255, 255));
-    void UnloadTexture(Texture texture);
-    int WindowShouldClose();
-    void CloseWindow();
-    void PollWindowEvents();
+    nothrow {
+        void SetTargetFPS(int fps);
+        void DrawText(const char* text, float x, float y, float scale, Color color = Color(255, 255, 255, 255));
+        float GetScreenWidth();
+        float GetScreenHeight();
+        void BeginDrawing();
+        void EndDrawing();
+        void ClearBackground(Color color);
+        void InitWindow(int argc, char** argv, int width, int height, const char* title);
+        void DrawRectangle(float x, float y, float width, float height, Color color = Color(255, 255, 255, 255));
+        Texture LoadTexture(const char* filename);
+        void DrawTexture(Texture texture, float x, float y, float scale, Color tint = Color(255, 255, 255, 255));
+        void DrawTexturePro(Texture texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint = Color(255, 255, 255, 255));
+        void UnloadTexture(Texture texture);
+        int WindowShouldClose();
+        void CloseWindow();
+        void PollWindowEvents();
+        float GetFrameTime();
+        float GetTime();
+        int IsKeyPressed(int key);
+        int IsKeyDown(int key);
+        void UpdateInput();
+    }
 }

+ 13 - 1
include/base.h

@@ -29,6 +29,14 @@ extern float screenHeight;
 #define ASSETS_PATH "assets/"
 #endif
 
+typedef struct {
+    float x, y;
+} Vector2;
+
+typedef struct {
+    float x, y, width, height;
+} Rectangle;
+
 void glInit(int* pargc, char** argv, int width, int height, const char* title);
 void glSwapBuffers();
 
@@ -47,4 +55,8 @@ void InitWindow(int argc, char** argv, int width, int height, const char* title)
 void SetWindowShouldClose(int shouldClose);
 int WindowShouldClose();
 
-void PollWindowEvents();
+void PollWindowEvents();
+
+int IsKeyPressed(int key);
+
+void UpdateInput();

+ 2 - 1
include/textures.h

@@ -24,4 +24,5 @@ typedef struct {
 
 void DrawTexture(Texture texture, float x, float y, float scale, Color color);
 Texture LoadTexture(const char* filename);
-void UnloadTexture(Texture texture);
+void UnloadTexture(Texture texture);
+void DrawTexturePro(Texture texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint);

+ 25 - 0
source/gl/base.c

@@ -141,4 +141,29 @@ void CloseWindow() {
     glfwDestroyWindow(window);
     glfwTerminate();
 }
+
+static int keyPreviousState[GLFW_KEY_LAST] = {0};
+static int keyCurrentState[GLFW_KEY_LAST] = {0};
+
+void UpdateInput() {
+    for (int i = 0; i < GLFW_KEY_LAST; i++) {
+        keyPreviousState[i] = keyCurrentState[i];
+        keyCurrentState[i] = glfwGetKey(window, i);
+    }
+}
+
+int IsKeyPressed(int key) {
+    if (key < 0 || key >= GLFW_KEY_LAST) return 0;
+    return (keyCurrentState[key] == GLFW_PRESS && keyPreviousState[key] == GLFW_RELEASE);
+}
+
+int IsKeyDown(int key) {
+    if (key < 0 || key >= GLFW_KEY_LAST) return 0;
+    return (keyCurrentState[key] == GLFW_PRESS);
+}
+
+int IsKeyReleased(int key) {
+    if (key < 0 || key >= GLFW_KEY_LAST) return 0;
+    return (keyCurrentState[key] == GLFW_RELEASE && keyPreviousState[key] == GLFW_PRESS);
+}
 #endif

+ 50 - 15
source/gl/textures.c

@@ -17,17 +17,8 @@
 
 Texture LoadTexture(const char* filename) {
     Texture texture = {0};
-    if (!filename) {
-        fprintf(stderr, "Error: NULL filename passed to LoadTexture\n");
-        return texture;
-    }
     stbi_set_flip_vertically_on_load(1);
     unsigned char* data = stbi_load(filename, &texture.width, &texture.height, &texture.channels, 0);
-
-    if (!data) {
-        fprintf(stderr, "Error: Could not load texture file %s\n", filename);
-        return texture;
-    }
     
     GLenum format;
     if (texture.channels == 4) {
@@ -36,7 +27,7 @@ Texture LoadTexture(const char* filename) {
         format = GL_RGB;
     } else {
         stbi_image_free(data);
-        fprintf(stderr, "гnsupported number of channels %d in texture\n", texture.channels);
+        fprintf(stderr, "unsupported number of channels %d in texture\n", texture.channels);
         return texture;
     }
     
@@ -82,11 +73,6 @@ Texture LoadTexture(const char* filename) {
 }
 
 void DrawTexture(Texture texture, float x, float y, float scale, Color tint) {
-    if (texture.id == 0 || texture.list_id == 0) {
-        printf("%s\n", "error: texture not loaded");
-        return;
-    }
-    
     glPushMatrix();
     glTranslatef(x, y, 0.0f);
     glScalef(texture.width * scale, texture.height * scale, 1.0f);
@@ -97,6 +83,55 @@ void DrawTexture(Texture texture, float x, float y, float scale, Color tint) {
     glPopMatrix();
 }
 
+// i do not recommend using this because of immediate mode.
+void DrawTexturePro(Texture texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint) {
+    float texWidth = (float)texture.width;
+    float texHeight = (float)texture.height;
+    float texLeft = source.x / texWidth;
+    float texRight = (source.x + source.width) / texWidth;
+    float texTop = source.y / texHeight;
+    float texBottom = (source.y + source.height) / texHeight;
+    texTop = 1.0f - texTop; // for inverting
+    texBottom = 1.0f - texBottom; // too
+    glPushMatrix();
+    glTranslatef(dest.x, dest.y, 0.0f);
+    if (rotation != 0.0f) {
+        glTranslatef(origin.x, origin.y, 0.0f);
+        glRotatef(rotation, 0.0f, 0.0f, 1.0f);
+        glTranslatef(-origin.x, -origin.y, 0.0f);
+    }
+    glScalef(dest.width, dest.height, 1.0f);
+    
+    glColor4f(tint.r / 255.0f, tint.g / 255.0f, tint.b / 255.0f, tint.a / 255.0f);
+    
+    glEnable(GL_TEXTURE_2D);
+    glBindTexture(GL_TEXTURE_2D, texture.id);
+    
+    if (texture.channels == 4) {
+        glEnable(GL_BLEND);
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    }
+    
+    glBegin(GL_QUADS);
+        glTexCoord2f(texLeft, texTop);
+        glVertex2f(0.0f, 0.0f);
+        glTexCoord2f(texRight, texTop);
+        glVertex2f(1.0f, 0.0f);
+        glTexCoord2f(texRight, texBottom);
+        glVertex2f(1.0f, 1.0f);
+        glTexCoord2f(texLeft, texBottom);
+        glVertex2f(0.0f, 1.0f);
+    glEnd();
+    
+    if (texture.channels == 4) {
+        glDisable(GL_BLEND);
+    }
+    
+    glDisable(GL_TEXTURE_2D);
+    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+    glPopMatrix();
+}
+
 void UnloadTexture(Texture texture) {
     if (texture.id != 0) {
         glDeleteTextures(1, &texture.id);