1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. To chat with the GameOgre community, you need to have at least 100 posts. Once you have the 100 posts, post at Become A New Ogre
    Dismiss Notice

php gd functions are cool

Discussion in 'Online Game Development' started by shugo, Apr 8, 2012.

Thread Status:
Not open for further replies.
  1. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    I recently discovered that my host has the gd functions installed on the server.

    This opens up so many possibilities.

    You can see an example here:

    6CULTURES

    part of the php script:

    Code:
    $im = imagecreatefrompng("117bw.png");
    $rgb = imagecolorat($im, 896, 850);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    
    $c = $r.", ".$g.", ".$b;
    
    echo "<P>rgb of 896, 850 = ".$c."</P>";
    
    
    Why is this cool

    Suppose I wanted a really big map and I want to have navies and armies on this map, but that some paths are blocked for these armies, and that navies obviously can't sail on land. I could do this manually, by making a huge database, but this would be an incredibly boring and tedious task, and it would take days if not weeks to complete this database.

    Not so with GD functions!

    Now all I have to do is draw a map with MS paint. Save it as a bitmap and use gimp to convert it to a png file. Then I can just fetch the rgb value of the pixel.

    For example: If the rgb value corresponds to blue, it is water, if it corresponds to green, it is land.

    Do I hear you say cool?!
     
    Last edited by a moderator: Apr 8, 2012
  2. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    Cool good luck
     
  3. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    I made a random version of this.

    Shugo 6
     
  4. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    I do notice the limits of these gd functions. They take a lot of time and computer resources to complete.

    Looping through a 1000 by 1000 pixels or more, one at a time is too much effort for the server it seems.

    I am currently looking for a way to count the number of pixels of a certain color in an image, but there doesn't appear to be such a GD function.
     
  5. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    by the way, there is also a native alternative version of GD as an php image editing library.

    Do you know what it is called?










    Cairo
     
    Last edited by a moderator: Apr 12, 2012
  6. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    ithink iwill use java for create map
     
  7. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
  8. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
  9. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    i am busy learning java myself


    I already tried to make an applet, but it doesn't appear to be working yet. I probably made a mistake with an import of a class, or there is some sort of browser security blocking the applet.

    Anyhow...
    Sofar I find that 2d isometric view of a village seems to me to be the better option for the townview in shugo. I could try to make it 3d, but this feels complicated even for me. There is so much non-algebraic mathematics in 3d and this makes things difficult for me. I wasn't good at working with tan, cosinus and sinus in highschool and I'm still no good at it now. So that makes me decide I should try 2d isometric sprites.

    I could try to make 2d isometric sprites with php and svg, but this would take a lot of layers. Even a small town of 4 by 4 grids, would take at least 7 layers. 5 by 5 requires 9 layers. Now imagine a 9 by 9 town, 17 layers is a lot.
     
  10. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    if you want you can try search from this book in ebookee.org may be you can find download url
     
  11. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    [​IMG]
    Killer Game Programming in Java (Repost) | Free eBooks Download - EBOOKEE!
    or
    [MULTI] Killer Game Programming in Java - Jiwang WareZ Scene | Free eBooks Download - EBOOKEE!

    may be you can find something in this book

    Over View
    Although the number of commercial Java games is still small compared to those written in C or C++, the market is expanding rapidly. Recent updates to Java make it faster and easier to create powerful gaming applications-particularly Java 3D-is fueling an explosive growth in Java games. Java games like Puzzle Pirates, Chrome, Star Wars Galaxies, Runescape, Alien Flux, Kingdom of Wars, Law and Order II, Roboforge, Tom Clancy's Politika, and scores of others have earned awards and become bestsellers.

    Java developers new to graphics and game programming, as well as game developers new to Java 3D, will find Killer Game Programming in Java invaluable. This new book is a practical introduction to the latest Java graphics and game programming technologies and techniques. It is the first book to thoroughly cover Java's 3D capabilities for all types of graphics and game development projects.

    Killer Game Programming in Java is a comprehensive guide to everything you need to know to program cool, testosterone-drenched Java games. It will give you reusable techniques to create everything from fast, full-screen action games to multiplayer 3D games. In addition to the most thorough coverage of Java 3D available, Killer Game Programming in Java also clearly details the older, better-known 2D APIs, 3D sprites, animated 3D sprites, first-person shooter programming, sound, fractals, and networked games. Killer Game Programming in Java is a must-have for anyone who wants to create adrenaline-fueled games in Java.
     
  12. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42

    I found a cumbersome way, to find out the number of pixels of a certain color in an image.

    STEP 1

    Open the bitmap in gimp

    STEP 2

    Save bitmap as an X PixMap (that's with a xpm extension)

    STEP 3

    Open the xpm with notepad

    STEP 4

    Replace the opening and closing of the rows with spaces (you basically remove them)

    Remove the top rows

    Replace the symbols that correspond to certain color values with letters in the alphabet

    STEP 5

    Save this as a txt file.

    STEP 6

    copy/paste all into the string that is the variable rgbstring

    Code:
    <?php
    
    $rgbstring = "";
    
    $afunc = substr_count($rgbstring, "a");
    
    echo "<P>a ".$afunc."</P>";
    
    ?>
    
    
    STEP 7

    Save the php file.

    STEP 8

    Open the php file to see how many there are.

    FINAL STEP

    Copy/repeat and alter the last two lines of the code for the other letters (i.e. other colors).

    EXPLANATION

    With that piece of code I count how many letter "a" there are. I know that "a" is a shade of green, so now I know how many pixels there are of that shade of green.

    IN CONCLUSION

    I've been able to figure out this way, that about 82% is blue = sea in my map. About 7% is darkgreen = forest, about 4% is gray = mountains, and about 6% is lightgreen = ricefield. The remaining 1% are other colors.
     
    Last edited by a moderator: Apr 13, 2012
  13. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    The xpm map saves the color values in hex.

    I found a hex to rgb convertor here:

    Hex-to-RGB Color Converter

    This makes it easier to figure out which letter is what color.
     
  14. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    good work idon't have experience in Gd function idon't try use it before ithink imust try use it but my problem with php ican't make game allow players see they units walk and run and fight so istudy java
     
  15. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    shouldn't really be a problem with php unless it's really big or you need lots of dynamic real time movement
     
  16. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    yes ineed lots of dynamic real time movement
     
  17. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    Hi shugo, i have question to you, how your map work? Sorry but ican't understand whats you try make in your map
     
  18. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    you mean the map mentioned in this thread?

    well I select a coordinate from the file, fetch the rgb value of the pixel and that's is how I know the color. Now suppose I fetch 25 of these pixels (5 x 5) next to eachother, that way I can make a grid appropriate to the map. The benefit of this is that I don't have to make the grid from fetching values from an sql database, because if the map is really big, that would mean, I need to make a really big database, which takes days to make.
     
  19. shugo

    shugo Elite Ogre Ogre Veteran

    Messages:
    2,093
    Likes Received:
    12
    Trophy Points:
    38
    Credit:
    15,378.42
    Example

    I made a nice little example, of the gd function.


    A working example can de found here:

    CAPOREGIME

    Here is the code:

    Code:
    
    
    echo "<P><A HREF='cityfetch.php'>click here to refresh</A></P>";
    
    $xrand = rand(1,663);
    $yrand = rand(1,1380);
      
    $im = imagecreatefrompng("city.png");
    
    echo "<P>top left corner is ".$xrand.",".$yrand."</P>";
    
    echo "<table>";
    
    
    $x = 0;
    
    while ($x <=6){
    
    echo "<tr>";
    
    $y = 0;
    
    while ($y <=6){
    $xr = $xrand + $x;
    $yr = $yrand + $y;
    
    $rgb = imagecolorat($im, $xr, $yr);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    $colo = $r.",".$g.",".$b;
    
    echo "<td>  ".$colo."  </td>";
    
    $y++;
    }
    
    $x++;
    
    echo "</tr>";
    
    }
    
    echo "</table>";
    
    echo "<IMG src='city.png' width='90%'>";
    
    
    
    
     
  20. rtkwar

    rtkwar Big Brute New Ogre

    Messages:
    142
    Likes Received:
    1
    Trophy Points:
    18
    Credit:
    298.66
    Thank you very much shugo iam busy this 2 week iwill visit this page in 14/6
     
Thread Status:
Not open for further replies.

Share This Page