0

I'm currently designing a Game Engine in C#. The engine has multiple driver types (OpenGL, DirectX). However each driver type requires different key codes when the user presses a button. But for some reason, using preprocessors isn't working... mind you I'm very new to using them.

I really think (like in most cases) the best way to get someone to understand what i'm trying to do is to show some code.

Here is my KeyCode enumeration (or part of it):

public enum KeyCode
{
#if DRIVER_TYPE_OPENGL
        A = 83,
        B = 84
#elif DRIVER_TYPE_DRIECTX
        A = 65,
        B = 66
#endif
}

At the top of my OpenGL Window class, I have done this:

#define DRIVER_TYPE_OPENGL

and at the top of my DirectX Window class, I have done this:

#define DRIVER_TYPE_DRIECTX

From what I understand, I should now be able to do something like this:

        IDisplay display = new OpenGLDisplay(WIDTH, HEIGHT, TITLE);

        while (true)
        {
            // Do stuff with display

            // Process input like so:
            if (Keyboard.IsKeyDown(KeyCode.A))
            {
                Console.WriteLine("You're using OpenGL driver, good on you!")
            }

            // Swap buffers and stuff
        }

        display.Close();

But instead, I get an error saying: "KeyCode does not contain a definition for 'A'"

What am I doing wrong?

Thanks in advance for any help!

  • Any particular reason why you are not using switch-case? – Souvik Ghosh Nov 29 '17 at 05:16
  • @SouvikGhosh Because I can't use switch-case inside an enumeration and I'd rather not have both "OpenGLKeyCode" & "DirectXKeyCode" enumerations. –  Nov 29 '17 at 05:18
  • @aaron that will only make the A value 65, and not allow me to switch between OpenGL & DirectX key code values. –  Nov 29 '17 at 05:22
  • @tdkr80 Where did you define your enum ? – Blacktempel Nov 29 '17 at 05:24
  • @Blacktempel it's defined outside of any classes and completely by it's self. –  Nov 29 '17 at 05:26
  • This might sound dumb but you don't have a different local variable named 'KeyCode' do you? Did you try testing the enum without the #if #elif #endif statement? – mattshu Nov 29 '17 at 05:27
  • @mattshu Blacktempel has just posted an answer that has helped me understand the problem. Hopefully this will clear things up for everyone. –  Nov 29 '17 at 05:29

1 Answers1

1

Move your enum or use project-wide defines.

The define will only work in the file you #define it.
This is C#, not C++ with headers.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53