Seite 297 von 340 ErsteErste ... 197 247 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 ... LetzteLetzte
Zeige Ergebnis 8.881 bis 8.910 von 10174

C/C++ Programming Help Thread

This is a discussion on C/C++ Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Zitat von yaustar Calling functions with infinite loops inside other infinite loops in functions is an EXTREMELY bad idea. You ...

  
  1. #8881
    QJ Gamer Blue
    Points: 4.008, Level: 40
    Level completed: 29%, Points required for next Level: 142
    Overall activity: 0%

    Registriert seit
    Dec 2007
    Ort
    dfgd
    Beiträge
    66
    Points
    4.008
    Level
    40
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von yaustar Beitrag anzeigen
    Calling functions with infinite loops inside other infinite loops in functions is an EXTREMELY bad idea. You eventual run out of stack memory and crash the game. In addition, it becomes nigh on impossible to exit cleanly in a sensible way.

    Use a state machine:

    (C code below)

    Code:
    #define MAIN_MENU 0
    #define MAIN_GAME 1
    
    int main()
    {
    	int currentState = MAIN_MENU;
    	int gameInProgress = 1;
    	
    	while( gameInProgress )
    	{
    		switch( currentState )
    		{
    		case MAIN_MENU:
    			{
    				if( /* player selects to play game * / )
    				{
    					currentState = MAIN_GAME;
    				}
    			} break;
    		case MAIN_GAME:
    			{
    			
    			} break;
    		default: break;
    		}
    	}
    	
    	return 0;		
    }
    Thanks for the heads up on that mistake then also that examplecode is really a good idea.
    Thanks for the nudge to better coding!:Jump:
    Luke



  2. #8882
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Technically, I should have used enums instead of #defines, but that is easy enough to change.

  3. #8883
    Developer
    Points: 3.206, Level: 35
    Level completed: 4%, Points required for next Level: 144
    Overall activity: 0%

    Registriert seit
    Aug 2007
    Beiträge
    74
    Points
    3.206
    Level
    35
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von michaelp Beitrag anzeigen
    Couldn't you use an enum instead of the preprocessor there?
    Code:
    int main()
    {
      enum state { MAIN_MENU, MAIN_GAME };
      state currentState = MAIN_MENU;
      return 0;
    }
    You still need to add enum in your variable declartions:

    Code:
    enum state { MAIN_MENU, MAIN_GAME };
    
    int main()
    {
      enum state currentState = MAIN_MENU;
      return 0;
    }
    Otherwise:

    Code:
    typedef enum _state
    { 
        MAIN_MENU, 
        MAIN_GAME, 
    
    } state;
    
    int main()
    {
        state currentState1 = MAIN_MENU;
    
        // or if you prefer to make it more explicit 
        // so people know it's an enumerated type
        // like what you do with typedef'd structs
        // struct _mystruct var instead of mystruct var
        // to make it unambiguous
        enum _state currentState2 = MAIN_MENU;
        return 0;
    }
    For state machines, enum's are most definitely preferred.
    PSP PRX LibDoc's Lives On!
    http://silverspring.lan.st/

    My new home:
    http://my.malloc.us/silverspring/

  4. #8884
    QJ Gamer Blue
    Points: 4.268, Level: 41
    Level completed: 59%, Points required for next Level: 82
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    497
    Points
    4.268
    Level
    41
    Downloads
    0
    Uploads
    0

    Standard

    I'm still learning C++, and I was wondering what is the best lib to use for 2D graphics???

  5. #8885
    QJ Gamer Silver
    Points: 14.087, Level: 77
    Level completed: 10%, Points required for next Level: 363
    Overall activity: 0%

    Registriert seit
    Jan 2006
    Ort
    Germany
    Beiträge
    926
    Points
    14.087
    Level
    77
    Downloads
    0
    Uploads
    0

    Standard

    There is no "best" in programming. It always just depends on what you need exactly.
    Raphs board rules #31: Excessive use of punctuation is either a sign of a lesser ego or a small mind. Avoid it if you don't want to look like a total moron.
    Raphs board rules #17: When you need to ask whether you are capable of doing something, you are not.
    Raphs board rules #2: Exploits aren't found by changing version numbers, blindly merging data into a file or turning your PSP upside down.
    Raphs board rules #1: If you have no clue how exploits work, don't come up with ideas about them.

  6. #8886
    QJ Gamer Blue
    Points: 4.268, Level: 41
    Level completed: 59%, Points required for next Level: 82
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    497
    Points
    4.268
    Level
    41
    Downloads
    0
    Uploads
    0

    Standard

    ok sorry. thanks for the info.

  7. #8887
    QJ Gamer Blue
    Points: 4.008, Level: 40
    Level completed: 29%, Points required for next Level: 142
    Overall activity: 0%

    Registriert seit
    Dec 2007
    Ort
    dfgd
    Beiträge
    66
    Points
    4.008
    Level
    40
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von PSProgramer Beitrag anzeigen
    I'm still learning C++, and I was wondering what is the best lib to use for 2D graphics???
    Im looking for a good 2d lib so if you get you hands on one then gimmie a bell id loveto have a look. thxz

  8. #8888
    OMFG
    Points: 19.453, Level: 88
    Level completed: 21%, Points required for next Level: 397
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Toronto
    Beiträge
    2.814
    Points
    19.453
    Level
    88
    Downloads
    0
    Uploads
    0

    Standard

    oslib

  9. #8889
    QJ Gamer Gold
    Points: 17.453, Level: 84
    Level completed: 21%, Points required for next Level: 397
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    everywhere
    Beiträge
    3.526
    Points
    17.453
    Level
    84
    Downloads
    1
    Uploads
    0

    Standard

    wat slasher said
    1. Failed....again...
    2. http://slicer.gibbocool.com/ stay updated on all my projects
    3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been

  10. #8890
    QJ Gamer Gold
    Points: 14.678, Level: 78
    Level completed: 57%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Nov 2006
    Beiträge
    1.523
    Points
    14.678
    Level
    78
    Downloads
    0
    Uploads
    0

    Standard

    I need a simplest example of how to blit an image using oslib. Any help?

  11. #8891
    lol
    Points: 20.859, Level: 91
    Level completed: 2%, Points required for next Level: 491
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    Whittier, CA
    Beiträge
    5.791
    Points
    20.859
    Level
    91
    Downloads
    0
    Uploads
    0

    Standard

    Code:
    int main() {
        oslInit(0);
        oslInitGfx(OSL_PF_8888,1);
        OSL_IMAGE * background = oslLoadImageFile("Background.png",OSL_IN_RAM,OSL_PF_8888);
        while (!osl_quit){
              oslStartDrawing();
              background->x = 0;
              background->y = 0;
              oslDrawImage(background);
              oslEndDrawing();
              oslSyncFrame();
        }
        oslEndGfx();
        oslWaitVSync();
        oslQuit();
        return 0;
    }
    That should work, its not tested.
    You might want to look into the samples and see how they put images on screen.

  12. #8892
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    Removed unneeded things, made it compilable, swizzle it for speed, unload resources for good practice and typically background images don't have an alpha channel so I changed the pixel format:
    Code:
    #include <oslib/oslib.h>
    
    PSP_MODULE_INFO("oslib", 0, 1, 1);
    PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
    
    int main()
    {
      oslInit(0);
      oslInitGfx(OSL_PF_8888,1);
      
      OSL_IMAGE *background = oslLoadImageFilePNG("Background.png", OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);
      
      while (!osl_quit){
            oslStartDrawing();
            
            oslDrawImage(background);
            
            oslEndDrawing();
            oslSyncFrame();
      }
      
      if (background != NULL)
        oslDeleteImage(background);
      
      oslEndGfx();
      oslQuit();
      
      return 0;
    }

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  13. #8893
    QJ Gamer Green
    Points: 5.495, Level: 47
    Level completed: 73%, Points required for next Level: 55
    Overall activity: 0%

    Registriert seit
    Aug 2008
    Ort
    Cyprus
    Beiträge
    48
    Points
    5.495
    Level
    47
    Downloads
    0
    Uploads
    0

    Standard Single action on button press

    Hi, I recently started C programming, and I have a question. What code should I use to make the psp execute code only once when I press a button?

  14. #8894
    QJ Gamer Blue
    Points: 4.268, Level: 41
    Level completed: 59%, Points required for next Level: 82
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    497
    Points
    4.268
    Level
    41
    Downloads
    0
    Uploads
    0

    Standard

    ok, really great place that helped me learn. psp-programming.com

    @SG57 - where are you telling the image the x and y of the screen that you are puting the image on???? or is that not included on this code cause it's a background?? if so then how would you define the x and y for a non-background image?? thanx.

  15. #8895
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    PSProgrammer - the OSL_IMAGE structure contains everything about the image (x, y, scale, rotation angle, offsets, image data, etc.). By default, when loading an image it's x and y values are initialized to 0. You could also use a different function that takes your own x and y values as arguments ignoring it's own members.

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  16. #8896
    QJ Gamer Blue
    Points: 4.268, Level: 41
    Level completed: 59%, Points required for next Level: 82
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    497
    Points
    4.268
    Level
    41
    Downloads
    0
    Uploads
    0

    Standard

    I'm really unfamiliar with oslib, and I'm still learning C++, but what would I need to put in the function to give me my own x and y variables???

  17. #8897
    QJ Gamer Green
    Points: 3.522, Level: 37
    Level completed: 15%, Points required for next Level: 128
    Overall activity: 0%

    Registriert seit
    May 2007
    Beiträge
    70
    Points
    3.522
    Level
    37
    Downloads
    0
    Uploads
    0

    Standard

    PSPProgramer, as SG57 said for example:

    Code:
    OSL_IMAGE *myImage;
    myImage now has x and y variables, along with the other variables mentioned by SG57. To use them you'd say for example:

    Code:
    myImage->x = 20;
    myImage->y = 50;
    which would set the image's x to 20 and y to 50. Hope that made sense.
    [url="http://www.safarial.homebrewheaven.net"]Blog[/url]

  18. #8898
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Do you know how to write a function that takes two parameters?

  19. #8899
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    Totally off topic post (since it's a learning experience for PSProgrammer so I won't post anything) but hey there yaustar :) I haven't talked directly to you in forever

    Think i can still occasionally PM you some C++ questions? I'd really like that

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  20. #8900
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von SG57 Beitrag anzeigen
    Totally off topic post (since it's a learning experience for PSProgrammer so I won't post anything) but hey there yaustar :) I haven't talked directly to you in forever

    Think i can still occasionally PM you some C++ questions? I'd really like that
    Sure, or you can post on Homebrew Illuminati where there is less 'noise'.

  21. #8901
    QJ Gamer Gold
    Points: 14.678, Level: 78
    Level completed: 57%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Nov 2006
    Beiträge
    1.523
    Points
    14.678
    Level
    78
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von SG57 Beitrag anzeigen
    Removed unneeded things, made it compilable, swizzle it for speed, unload resources for good practice and typically background images don't have an alpha channel so I changed the pixel format:
    Code:
    #include <oslib/oslib.h>
    
    PSP_MODULE_INFO("oslib", 0, 1, 1);
    PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
    
    int main()
    {
      oslInit(0);
      oslInitGfx(OSL_PF_8888,1);
      
      OSL_IMAGE *background = oslLoadImageFilePNG("Background.png", OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);
      
      while (!osl_quit){
            oslStartDrawing();
            
            oslDrawImage(background);
            
            oslEndDrawing();
            oslSyncFrame();
      }
      
      if (background != NULL)
        oslDeleteImage(background);
      
      oslEndGfx();
      oslQuit();
      
      return 0;
    }
    Is there LoadimageMemory for loading PNGs?

  22. #8902
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    Yes, there are two methods actually. The file type doesn't matter but in this case i'll use PNG

    One uses temporary file data:
    Code:
    //Binary data representing a PNG file
    const int test_data[] = {...};
    //The size of the above array data
    const int test_data_size = ???;
    //Set data for the temporary file
    oslSetTempFileData(test_data, test_data_size, &VF_MEMORY);
    //Load a PNG file using the temporary file (oslGetTempFileName to get its name)
    OSL_IMAGE* image = oslLoadImageFilePNG(oslGetTempFileName(), OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);
    and the other a virtual file system:
    Code:
    //Binary data representing a PNG file
    const int test_png[] = {...};
    
    //Each entry consists of a name, a pointer to the data, the size of the data, and a file type.
    OSL_VIRTUALFILENAME ram_files[1] =
    {
            {"ram:/test.png", (void*)test_png, sizeof(test_png), &VF_MEMORY}
    };
    
    //Add these files to the list
    oslAddVirtualFileList(ram_files, oslNumberof(ram_files));
    
    //We can now open them as if they were real files...
    OSL_IMAGE *img = oslLoadImageFilePNG("ram:/test.png", OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  23. #8903
    QJ Gamer Gold
    Points: 14.678, Level: 78
    Level completed: 57%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Nov 2006
    Beiträge
    1.523
    Points
    14.678
    Level
    78
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von SG57 Beitrag anzeigen
    Yes, there are two methods actually. The file type doesn't matter but in this case i'll use PNG

    One uses temporary file data:
    Code:
    //Binary data representing a PNG file
    const int test_data[] = {...};
    //The size of the above array data
    const int test_data_size = ???;
    //Set data for the temporary file
    oslSetTempFileData(test_data, test_data_size, &VF_MEMORY);
    //Load a PNG file using the temporary file (oslGetTempFileName to get its name)
    OSL_IMAGE* image = oslLoadImageFilePNG(oslGetTempFileName(), OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);
    and the other a virtual file system:
    Code:
    //Binary data representing a PNG file
    const int test_png[] = {...};
    
    //Each entry consists of a name, a pointer to the data, the size of the data, and a file type.
    OSL_VIRTUALFILENAME ram_files[1] =
    {
            {"ram:/test.png", (void*)test_png, sizeof(test_png), &VF_MEMORY}
    };
    
    //Add these files to the list
    oslAddVirtualFileList(ram_files, oslNumberof(ram_files));
    
    //We can now open them as if they were real files...
    OSL_IMAGE *img = oslLoadImageFilePNG("ram:/test.png", OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);

    Thanks a lot SG! :)

  24. #8904
    QJ Gamer Blue
    Points: 4.268, Level: 41
    Level completed: 59%, Points required for next Level: 82
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    497
    Points
    4.268
    Level
    41
    Downloads
    0
    Uploads
    0

    Standard

    yeah thank you for the help.

  25. #8905
    Points: 2.658, Level: 31
    Level completed: 39%, Points required for next Level: 92
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    47
    Points
    2.658
    Level
    31
    Downloads
    0
    Uploads
    0

    Standard

    PSProgrammer - What you have there is a pointer to a struct. If you look up 'struct' on google you can find thousands of tutorials explaining their declaration and use. When you have just a 'normal' struct you can refer to its members by 'name_of_struct.name_of_m ember' whereas if you have a pointer to a struct, as is the case here then you can use 'name_of_struct->name_of_member' rather than '(*name_of_struct).name_o f_member'. Looks much nicer.

    Structs are an invaluable asset in C/C++ programming. Good luck, mate. :)

  26. #8906
    QJ Gamer Green
    Points: 4.092, Level: 40
    Level completed: 72%, Points required for next Level: 58
    Overall activity: 0%

    Registriert seit
    Jul 2008
    Beiträge
    508
    Points
    4.092
    Level
    40
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von kostakis Beitrag anzeigen
    Hi, I recently started C programming, and I have a question. What code should I use to make the psp execute code only once when I press a button?
    (how I do it is)You would grab a variable, and have it when no buttons are pressed set to 0. Then, under your code to exec...ahh..screw it...here's a sample:
    Code:
    if(pad.Buttons & PSP_CTRL_CROSS)
    {
    if(!buttonBool0)
    {
    //CODE RIGHT IN HERE
    }
    buttonBool0=1;
    }
    else buttonBool0=0;
    That should work, just make sure to declare it. There are probably simpler methods, but, this isn't one of them.

  27. #8907
    QJ Gamer Green
    Points: 5.495, Level: 47
    Level completed: 73%, Points required for next Level: 55
    Overall activity: 0%

    Registriert seit
    Aug 2008
    Ort
    Cyprus
    Beiträge
    48
    Points
    5.495
    Level
    47
    Downloads
    0
    Uploads
    0

    Standard

    Thanks, I will test it after my virtualbox problem is solved. It's been three days I cannot access my source code from it and compile it :Argh:

  28. #8908
    QJ Gamer Silver
    Points: 14.087, Level: 77
    Level completed: 10%, Points required for next Level: 363
    Overall activity: 0%

    Registriert seit
    Jan 2006
    Ort
    Germany
    Beiträge
    926
    Points
    14.087
    Level
    77
    Downloads
    0
    Uploads
    0

    Standard

    Or alternatively, in case you have a lot of different button presses you want to handle and not have a variable for each button:

    Code:
    int lastButtons = 0;
    
    while(isrunning)
    {
       sceCtrlReadData(&pad);
       int pressed = ~lastButtons & pad.Buttons; // only buttons that have been pressed down this frame
       int released = lastButtons & ~pad.Buttons; // in case you want to react on the buttons being released rather than pushed
       if(pressed & PSP_CTRL_CROSS)
       {
           ... code that will only be executed the instant the button gets pushed down
       }
       lastButtons = pad.Buttons;
    }
    Geändert von Raphael (08-16-2008 um 04:28 AM Uhr)
    Raphs board rules #31: Excessive use of punctuation is either a sign of a lesser ego or a small mind. Avoid it if you don't want to look like a total moron.
    Raphs board rules #17: When you need to ask whether you are capable of doing something, you are not.
    Raphs board rules #2: Exploits aren't found by changing version numbers, blindly merging data into a file or turning your PSP upside down.
    Raphs board rules #1: If you have no clue how exploits work, don't come up with ideas about them.

  29. #8909
    QJ Gamer Blue
    Points: 4.268, Level: 41
    Level completed: 59%, Points required for next Level: 82
    Overall activity: 0%

    Registriert seit
    Apr 2008
    Beiträge
    497
    Points
    4.268
    Level
    41
    Downloads
    0
    Uploads
    0

    Standard

    Hey thanks for al the help Khatharr and SG57, I got alot more done. Also I find now that I quite like OSLib

  30. #8910
    QJ Gamer Gold
    Points: 14.678, Level: 78
    Level completed: 57%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Nov 2006
    Beiträge
    1.523
    Points
    14.678
    Level
    78
    Downloads
    0
    Uploads
    0

    Standard

    Donot we need to do oslSwapBuffer after enddrawing syncframe like flipscreen()?


 

Tags for this Thread

Forumregeln

  • Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
  • Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
  • Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
  • Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
  •  





Alle Zeitangaben in WEZ -8. Es ist jetzt 09:15 PM Uhr.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © , Caputo Media, LLC. All Rights Reserved. Cluster .