Thread: Why does this keep frezzing up when I run it?

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Server + One that allows input:


    Code:
    #include <SDL.h>
    #include <socket.cpp>
    #include <iostream>
    #include <sstream>
    SDL_Event event;
    SDL_Surface *screen;
    ServerSocket server;
    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();
    
        //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()
    {
        //If a key was pressed
        if( event.type == SDL_KEYDOWN )
        {
            //Adjust the velocity
            switch( event.key.keysym.sym )
            {
                case SDLK_UP: y -= 5; break;
                case SDLK_DOWN: y += 5; break;
                case SDLK_LEFT: x -= 5; break;
                case SDLK_RIGHT: x += 5; break;
            }
        }
    }
    
    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 ) );
    }
    char axw[11];
    
    int Dot::getx(){
        return x;
    }
    int Dot::gety(){
        return y;
    }
    
    int main(int argc, char* args[])
    {
        server.StartHosting(4753);
        
            SDL_Init(SDL_INIT_EVERYTHING);
        screen = SDL_SetVideoMode(500,500,32,0);
        clearscreen();
    Dot mydot;
    Dot hisdot;
    bool quit = false;
    while(quit == false){
    while(SDL_PollEvent(&event)){
    if(event.type == SDL_QUIT){
                  quit = true;
                  }
    
    
    
    
    
    mydot.handle_input();
    clearscreen();
    mydot.show();
    SDL_Flip(screen);
    
    int xz = mydot.getx();
    int yz = mydot.gety();
    
    /*
    char* sxz,*syz;
    itoa(xz,sxz,10);
    itoa(yz,syz,10);
    
    string az = sxz;
    string bz = syz;
    
    string hae = az + " BA " + bz;
    
    char *ha;
    
    strcpy(ha,hae.c_str());
    
    server.SendData(ha);
    */
    stringstream oout;
    oout << xz;
    stringstream ooutz;
    ooutz << yz;
    
    string az = oout.str();
    string bz = ooutz.str();
    
    string zzz = az + " BA " + bz;
    
    
    memcpy(axw," ",11);
    
    strcpy(axw,zzz.c_str());
    
    
    
    }
    SDL_Delay(100);
    server.SendData(axw);
    SDL_Delay(100);
    }
    }

    Client / one that shows what the guy with the server did:

    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("127.0.0.1",4753);
        
          SDL_Init(SDL_INIT_EVERYTHING);
        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);
    }
    
    }

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Those are both the programs code.
    I belive you saw the sockets.h code.


    ...


    I might just remake them completly.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Have you tried running in a debugger to see where it's stopping? Or added print statements at least to try to pinpoint?

    Also, where's your ClientSocket code?

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    I do not even know what printf is.
    But I can guess...

    Does it print down to where the errors show up?



    And do I have to download a debugger or is it in my IDE?

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I would say that if you can't discover those two on your own, there is little hope of successfully writing a real time client server graphics application.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    That dosn't affect me, as I CAN discover that on my own.

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Well, your IDE should have a debugger - set a break point early in the code and step through it. That would be preferable to putting print statements everywhere.

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    I think I got something basic to work!

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, 04:56 PM