Regexes and Shader Abstraction | Coding a 2D Game Engine in Java #6

Поделиться
HTML-код
  • Опубликовано: 16 апр 2020
  • Join the Discord: / discord
    In this tutorial for coding a 2D game engine in Java using LWJGL, I go over how to use regexes to find patterns, which we then use to parse a file that contains a vertex and fragment shader. We then abstract all the shader code written in the previous tutorial into one Shader class, that way we can have easy to use Shader objects that take care of compilation and identification numbers for us.
    Code for this video: github.com/codingminecraft/Ma...
    ---------------------------------------------------------------------
    Website: ambrosiogabe.github.io/
    Github: github.com/ambrosiogabe
    Here are some books I recommend if you want to learn about game engine development more thoroughly. I do not profit off any of these sales, these are just some books that have helped me out :)
    My Recommended Game Engine Books:
    Game Engine Architecture: www.gameenginebook.com/
    Game Physics Cookbook (Read this before the next physics book): www.amazon.com/Game-Physics-C...
    Game Physics (Ian Millington): www.amazon.com/Game-Physics-E...
    Game Programming Patterns (Free): gameprogrammingpatterns.com/
    My Recommended Beginning Game Programming Books:
    JavaScript Game Design: www.apress.com/gp/book/978143...
    My Recommended Java Books:
    Data Structures/Algorithms: www.amazon.com/Data-Structure...
    LWJGL (Free, but I haven't read this thoroughly): lwjglgamedev.gitbooks.io/3d-g...
    Outro Music: www.bensound.com/royalty-free...

Комментарии • 70

  • @vintyprod
    @vintyprod 2 года назад +11

    With every tutorial I follow I just continue to be stunned at the quality of content available for free. Thank you for making these.

    • @GamesWithGabe
      @GamesWithGabe  2 года назад

      Thanks Ody! I truly appreciate this comment, and it really encourages me :). I will definitely admit that the series degrades over time, but I'm hoping to increase the quality for the next series. Thanks again!

  • @gower1973
    @gower1973 2 года назад +9

    For anyone watching this in 2022 and you are getting the non zero exit value error, have a look at you regex line, you need to have the space between the second set of brackets after (#type) for it to work.

  • @asherhaun
    @asherhaun 4 года назад +14

    note for linux (or mac os) users, using "
    " in your eol will result in an error. so just use "
    ".

    • @giverplay
      @giverplay 3 года назад +3

      Nice. System.lineSeparator() is useful.

    • @azatakhunov6061
      @azatakhunov6061 3 года назад

      Macos too

    • @asherhaun
      @asherhaun 3 года назад

      @@azatakhunov6061 thanks for letting me know, I will edit my comment for mac users as well.
      I guess it is because both systems are *nix based so they use the same line endings.

    • @asherhaun
      @asherhaun 3 года назад +1

      @@giverplay I didn't know that was a method, but that is nice to know about.

    • @chrisjude9933
      @chrisjude9933 3 года назад

      i guess I am quite randomly asking but do anybody know of a good website to stream new movies online ?

  • @m4rt_
    @m4rt_ 2 года назад +3

    Thanks for making these amazing videos :) Planing on possibly in the end add some 3d compatibility to the game engine and making a game

  • @davidkobrin9581
    @davidkobrin9581 3 года назад +12

    hey thanks for this series, I took the parser a step further to allow for additional types of shaders in the future using and array of strings and a switch statement to parse they shader type...
    public Shader(String filepath) {
    this.filepath = filepath;
    String source = null;
    try {
    source = new String(Files.readAllBytes(Paths.get(filepath)));
    } catch (IOException e) {
    e.printStackTrace();
    assert false : "Error could not open file for shader : '" + filepath +"'";
    }
    String[] splitString = source.split("(#type)( )+([a-zA-Z]+)");
    if (splitString.length < 2) {
    assert false : "Error shader '" + filepath + "' is not a valid shader";
    }
    String[] shadertype = new String[splitString.length-1];
    int count = 1;
    int startPos = 0;
    int endPos = 0;
    while (count < splitString.length) {
    startPos = source.indexOf("#type", endPos) + 6;
    endPos = source.indexOf("
    ", startPos);
    shadertype[count-1] = source.substring(startPos, endPos).trim();
    switch (shadertype[count-1]) {
    case "vertex":
    vertexSrc = splitString[count];
    //System.out.println("vertex source =
    " + vertexSrc);
    break;
    case "fragment":
    fragmentSrc = splitString[count];
    //System.out.println("fragment source =
    " + fragmentSrc);
    break;
    default:
    assert false : "Error shader '" + filepath + "' has invalid types";
    }
    ++count;
    }
    }

    • @GamesWithGabe
      @GamesWithGabe  3 года назад +2

      Nice David! I hope I'll get to explore geometry shaders one day and then I can extend the shader code like yours to add support for geometry shaders :)

  • @VoylinsLife
    @VoylinsLife 2 года назад +2

    The last 2 episodes are quite a big sandwich. The most confusing parts are that for certain things you need to keep in mind how c/c++ works. But I got more of an understanding now than I did the first time when I tried this.
    I hope one day I will fully understand how the VAO, VBO, EBO, IDO work xD

  • @pranavshishodia8953
    @pranavshishodia8953 2 года назад

    Holy fu*k your videos are one of the best I have seen in my life. Been trying to search these kinds for like 1 week and you come up and save the day. Your videos are awesome asf. Thanks! You earned a sub +1 😊

  • @m4rt_
    @m4rt_ 2 года назад +3

    Note: to add some cross platform compatibility for the "
    " and "
    " I ended up putting try catch statements around them and first doing the windows one in try then Linux/mac in catch and it works.
    This works because how a try catch works is that it tries to run some code and if it gets error it runs the code in catch
    ( I used the exception "Exception" for this)

  • @Snowmanver2
    @Snowmanver2 2 года назад

    like the regex part, thanks!

  • @alexandruantoci2691
    @alexandruantoci2691 3 года назад +4

    I LOVE YOU MAN, U ARE JUST MAKING MY LIFE FUCKING HARDER AND CONFUSING BUT I HAD TO DO A GAME IN JAVAFX, AND I LIKE TO DO COMPLICATED THINGS, AND I PREFER WHATCHING YOU MUCH MORE THAN JESSA RHOADES. LOVE FROM ITALY AND MOLDOVA.

  • @muggish19
    @muggish19 2 года назад +1

    I believe "(#type).*" is a simpler regular expression that achieves the same effect.

  • @brinklebros7136
    @brinklebros7136 3 месяца назад

    dang you make this stuff like your on speed lol one day i want to be able to write like you.

  • @developer2
    @developer2 2 года назад +1

    On Unix systems if you're getting a StringIndexOutOfBoundsException get rid of the
    in the eol variable.

  • @nloyolayt
    @nloyolayt 4 года назад +9

    Why not split up default.glsl into two separate files: one for the vertex shader and the other for the fragment? That way you don't have to parse the files using Regexes?

    • @GamesWithGabe
      @GamesWithGabe  4 года назад +10

      Hey Nelson, if you want to separate the code into two different files that is perfectly valid. The reason I use the regexes and everything is because even though there is some overhead in setting it up, it pays off in the long run to be able to see your vertex and fragment shader in one file since you usually modify both of them at the same time :)

    • @Igrium
      @Igrium Год назад

      @@GamesWithGabe Interesting. Personally, I went with the two separate files approach as well so that it would be compatible with glsl linters and validators. And it's rather trivial to just open two editors side-by-side imo.

  • @a1silver_
    @a1silver_ 2 года назад +3

    16:45 only the vertex shader source is showing (twice)

  • @avinashgupta8955
    @avinashgupta8955 Год назад

    I've followed the exact same steps of this tutorial and all the tutorials in the playlist but for some reason, the rectangle is not showing on the screen please reply so I can fix this.

  • @ade5324
    @ade5324 Год назад

    Is it possible to do shader source code file splitting in compile time, instead of runtime in java?

    • @GamesWithGabe
      @GamesWithGabe  Год назад

      Yep, although the savings you would get in performance are negligible. The most costly part of shaders ime, has been uploading the shader to the GPU and compiling the shader. So you could find a way to split it at compile time, but it probably isn't worth it

  • @ncg-sg4nb
    @ncg-sg4nb 3 года назад

    this String source = new String(Files.readAllBytes(Paths.get(filePath))); is am error?

    • @GamesWithGabe
      @GamesWithGabe  3 года назад

      Hey @3010ncg. Can you elaborate? That line of code is fine, it's just that if the file doesn't exist you will get an error

  • @mackbrooks2570
    @mackbrooks2570 3 года назад +1

    Hello @GamesWithGabe I've got no errors with my code! BTW I like the tutorials (very educational). But I still render a white screen with no shaded rectangle. What could be the cause of this? It was the same with Tutorial number 5! But I'm still grateful. Please reply soon.

    • @GamesWithGabe
      @GamesWithGabe  3 года назад

      Hey Mack, this could be quite a few different things. I suggest that you take a look at my debugging video first because that my help you find the problem: ruclips.net/video/Oh7yLrNYttk/видео.html there's another longer video too, but I think this one should catch a lot of bugs. If you still can't solve it reply again with a link to a GitHub repository and I can check it out :)

    • @NikitaKolosov
      @NikitaKolosov 3 года назад +1

      Maybe it help someone
      I catch the same problem because I created new shaderProgramID variable in compile() function and it conflicts with the same variable in class. Check that shaderProgramID that you send to glUseProgram() in use() is correct.

    • @indelton7465
      @indelton7465 2 года назад +1

      @@NikitaKolosov Thank you. This surely helped me!

    • @pianintendo
      @pianintendo Год назад

      @@NikitaKolosov One year after I had this exact problem for this exact reason, I initiated a shaderProgramID variable twice. Thanks !

  • @smipealex3173
    @smipealex3173 2 года назад

    Okey, on unix systems, the new line is simply '
    ', only on Windows the new line is '
    '

  • @ilanbitan8764
    @ilanbitan8764 3 года назад

    I did in the same way you did but i got an error.. i debugged and saw that the problem is with this line ->
    Shader testShader = new Shader("assert/shaders/default.glsl");
    it from the LevelEditorScene constructor and i saw that java "catch" the ioException
    how can i fix it? or there is another way?

    • @GamesWithGabe
      @GamesWithGabe  3 года назад

      Hey Ilan sorry about the late response, its been a busy couple past days :) . This looks like it may have some trouble finding your shader (I just noticed you have a typo there it says 'assert/shaders/default.glsl' when it should say 'assets...' if that was just from copying it into RUclips try the next bit of my comment), do you have some experience with file input/output in Java? If you do, write a text file from your code and see where Java writes the text file into your file system. That is your current working directory, now create an assets directory in that directory and create a shaders directory in there. Then put your shader there and see if that fixes the issue. If not I may need to take a closer look at your code :)

    • @ilanbitan8764
      @ilanbitan8764 3 года назад

      @@GamesWithGabe i learned java but i dont have an experience on input/output
      i changed the word 'assert' to 'assets'
      but it keep gives me and error
      i hope if you could help me here
      i wrote the code in the same way you wrote and im not sure how to fix it

    • @GamesWithGabe
      @GamesWithGabe  3 года назад

      Hey Ilan why don't you try to write a file using one of the methods in this question stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java . Then just name the file something like test.txt and then follow the rest of my instructions from the last comment and see if that helps

    • @ilanbitan8764
      @ilanbitan8764 3 года назад

      @@GamesWithGabe but your file in the video is .glsl so how can i work with it?
      i need to create a new file? and give the path to that file?

    • @GamesWithGabe
      @GamesWithGabe  3 года назад

      Well I'm just trying to figure out what the problem is right now. The file extension actually doesn't matter, but I'm assuming your code cant find the file. Without the error message I don't actually know what the problem is, do you have the error by any chance?

  • @lastyhopper2792
    @lastyhopper2792 2 года назад

    20:45
    instead of getting that cool
    "0(8) : error C0000: syntax error, unexpected reserved word bla-bla-bla,"
    I only get this lame:
    "ERROR: 0:10: 'void' : syntax error syntax error"
    Otherwise everything work just fine :)

  • @Bkha1250
    @Bkha1250 2 года назад

    For those who get the Process 'command 'C:/Program Files/Java/jdk-17.0.1/bin/java.exe'' finished with non-zero exit value 1 error, you have a typo. I spent an hour looking for ways to fix my issue but when I rewatched the video and carefully followed every second, I saw that I typed () instead of ( ), which crashed my entire program. Good luck finding your typo.

  • @serkanefe3334
    @serkanefe3334 3 года назад

    .jdks/corretto-11.0.11/bin/java.exe'' finished with non-zero exit value 1 I am taking this error over and over again what is this.

    • @GamesWithGabe
      @GamesWithGabe  3 года назад

      This is an error that is occurring within the OpenGL binding's or the GLFW binding's. So this means something went wrong in the C++ code, but Java just quits and doesn't give you any information about what happened. I have a couple of videos about debugging these types of problems (I think it's called debugging LWJGL or something like that), but if you know how to use the IDE's debugger, I would suggest stepping through your program and seeing where it crashes. This should narrow your search and you should be able to find the bad line of code and fix it :)