Why does this keep frezzing up when I run it?

This is a discussion on Why does this keep frezzing up when I run it? within the C++ Programming forums, part of the General Programming Boards category; No errors when compiling and I can comment out most of the main function and it still frezzes. Remove the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    Why does this keep frezzing up when I run it?

    No errors when compiling and I can comment out most of the main function and it still frezzes.
    Remove the SDL parts and it won't freeze up but then again the SDL is most of the program.


    CODE:

    Code:
    //Main.cpp
    #include <SDL.h>
    #include <iostream>
    #include "Socket.h"
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    SDL_Surface *screen;
    SDL_Event event;
    
    void apply_surface( int x, int y, SDL_Surface* source)
    {
        //Make a temporary rectangle to hold the offsets
        SDL_Rect offset;
        
        //Give the offsets to the rectangle
        offset.x = x;
        offset.y = y;
        //Blit the surface
        SDL_BlitSurface( source, NULL, screen, &offset );
        
        SDL_Flip(screen);
        
        
    }
    
    void clearscreen(){
                SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    class Dot
    {
        private:
        //The X and Y offsets of the dot
        int x, y;
    
    
    
        public:
        //Initializes the variables
        Dot();
    
        //Takes key presses and adjusts the dot's velocity
        void handle_input(int xz, int yz);
    
        //Moves the dot
        void move();
    
        //Shows the dot on the screen
        void show();
        
        int getx();
        int gety();
    };
    
    
    
    
    
    
    Dot::Dot()
    {
        //Initialize the offsets
        x = 0;
        y = 0;
    
    }
    
    
    
    void Dot::handle_input(int xz,int yz)
    {
    x = xz;
    y = yz;
    }
    
    void Dot::show()
    {
    SDL_Rect basd;
    basd.w = 10;
    basd.h = 10;
    basd.y = y;
    basd.x = x;
    SDL_FillRect( screen, &basd, SDL_MapRGB( screen->format, 0x00, 0x99, 0x00 ) );
    }
    
    
    int Dot::getx(){
        return x;
    }
    int Dot::gety(){
        return y;
    }
    
    
    int main(int argc, char* args[])
    {
                  screen = SDL_SetVideoMode(500,500,32,0);
          //Dot ase;
        int choice;
        int port = 4521;
        //char *ipAddress = "127.0.0.1";
        string ipAddress;
        bool done = false;
        char recMessage[STRLEN];
        char sendMessage[STRLEN];
    
    
            //Client
    
            
            ClientSocket sockClient;
    
            sockClient.ConnectToServer("127.0.0.1", 4753 );
    
    
            //Connected
    
    //74.76.149.190
    
              string jppa;
              int pos1,pos2;
    
    
    
    
            while ( true )
            {     
    
    sockClient.RecvData(recMessage,STRLEN);
    string sad = recMessage;
    
    if(strlen(sad.c_str()) > 6){
    
    int pos = sad.find("BA"); 
    string xc,yc;
    
    xc = sad.substr(0,3); 
    yc = sad.substr(pos+3);
    
    int ll,kk;
    
    ll = atoi(xc.c_str());
    kk = atoi(yc.c_str());
    
    
    }
    
    
    SDL_Delay(150); 
    }
    
    
    
            }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see, in your main program, where it does anything that you can see. You're constantly getting messages, and searching through messages, but.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    There is a delay of 150 milliseconds on the loop.
    That should slow it down decently.

    It sends nothing.
    Recives some.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why should it send anything? I don't see any send functions in the code either.

    (And just for reference -- why use strlen on a std::string? It has a size() built in.)

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    So, I'd use:

    string bob = "HI";
    int saize = bob.size();

    For strlen from now on?


    Also there are NO send functions in it, that is correct.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by azjherben View Post
    So, I'd use:

    string bob = "HI";
    int saize = bob.size();

    For strlen from now on?
    That is correct.
    Quote Originally Posted by azjherben View Post
    Also there are NO send functions in it, that is correct.
    Then I guess that's why it wasn't sending anything.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    It's not supposted to.
    It crashes, like it frezzes up and I gotta get task manager to close it.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not sure what exactly you want to happen here. You have written a program that doesn't do anything (including stop) and therefore that's what it does. What do you want it to do?

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Here is my UPDATED PROGRAM:
    (way better; actually runs; but still frezzes after 5-10 seconds)

    Code:
    #include <Socket.h>
    #include <SDL.h>
    #include <iostream>
    #include <sstream>
    SDL_Event event;
    SDL_Surface *screen;
    ClientSocket client;
    void apply_surface( int x, int y, SDL_Surface* source)
    {
        //Make a temporary rectangle to hold the offsets
        SDL_Rect offset;
        
        //Give the offsets to the rectangle
        offset.x = x;
        offset.y = y;
        //Blit the surface  
        SDL_BlitSurface( source, NULL, screen, &offset );
        
        SDL_Flip(screen);
        
        
    }
    
    void clearscreen(){
                SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    class Dot
    {
        private:
        //The X and Y offsets of the dot
        int x, y;
    
    
    
        public:
        //Initializes the variables
        Dot();
    
        //Takes key presses and adjusts the dot's velocity
        void handle_input(int xx,int yy);
    
        //Moves the dot
        void move();
    
        //Shows the dot on the screen
        void show();
        
        int getx();
        int gety();
    };
    
    
    
    
    
    
    Dot::Dot()
    {
        //Initialize the offsets
        x = 0;
        y = 0;
    
    }
    
    
    
    void Dot::handle_input(int xx,int yy)
    {
    y = yy;
    x = xx;
    }
    
    void Dot::show()
    {
    SDL_Rect basd;
    basd.w = 10;
    basd.h = 10;
    basd.y = y;
    basd.x = x;
    SDL_FillRect( screen, &basd, SDL_MapRGB( screen->format, 0x00, 0x99, 0x00 ) );
    }
    
    
    int Dot::getx(){
        return x;
    }
    int Dot::gety(){
        return y;
    }
    
    int main(int argc, char* args[])
    {
    string aaw;
        char coords[11];
       client.ConnectToServer("74.76.147.199",4753);
        
        screen = SDL_SetVideoMode(500,500,32,0);
        clearscreen();
    int zx,zy;
    Dot mydot;
    Dot hisdot;
    bool quit = false;
    
    
    
    
    
    while(quit == false){
    zx = 50;
    zy = 50;
    for(int p = 1; p<500; p++){
    
    
    aaw = coords;
    
    if (aaw.size() > 4){
    
    //                100 BA 100
    
    int pos = aaw.find("BA");
    string px1 = aaw.substr(0,3);
    string px2 = aaw.substr(pos+3,3);              
    
    zx = atoi(px1.c_str());
    zy = atoi(px2.c_str());
    }
    
    if(zx < 3){
          zx = 150;
          }
    
    if(zy < 3){
          zy = 150;
          }
    }
    mydot.handle_input(zx,zy);
    clearscreen();
    mydot.show();
    SDL_Flip(screen);
    
    
    
    SDL_Delay(100);
    
    
    client.RecvData(coords,11);
    
    SDL_Delay(100);
    }
    
    }

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Maybe put an SDL_Quit at the end? It also kills the window.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You need to call SDL_Init before any other SDL function.
    Last edited by medievalelks; 05-31-2009 at 06:20 AM.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    I tried SDL_Quit()...
    I'll add SDL_Init() ...

    Think that will help at all?

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    We're all on pins and needles awaiting your test :-)

    Yes, I think that should help - the docs say it is required, so it probably is for good reason.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    SDL_Init() is a must, lol.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    I'm sorry to say that just adding the function SDL_Init(blah) right before I set up the screen (screen = SDL_SETVIDEOMODE(blah,blah,blah,blah)) did little to help.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  2. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  4. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  5. Replies: 2
    Last Post: 10-29-2002, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21