CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to get the following Mode 13 H C Code to work in Visual Studio 2015 Pro ???

  1. #1
    Join Date
    Apr 2016
    Posts
    2

    How to get the following Mode 13 H C Code to work in Visual Studio 2015 Pro ???

    A Very Good Day to you SIR !!!

    I hope you are doing Great SIR !!!


    May I please kindly ask if the below C Code for Mode 13 H VGA Mode will work in Visual Studio 2015 Pro ???

    I tried to compile it in Visual C++ 2015 and had some errors that referred to the "Unsigned Char Far *" Statement

    And it also complained about the Register Calls being error nous

    May I please kindly ask if any kind soul can look through the below code and make it compile and run on Visual Studio 2015 as well as Windows 10 Pro 64Bit ?

    Hope to hear from you soon.

    Thank you very much for your time and kind help !!!

    Warm Regards as Always !!!

    Dr. David Bowman


    Code:
    // I N C L U D E S ///////////////////////////////////////////////////////////
    
    #include <conio.h>
    #include <stdio.h>
    #include <process.h>
    #include <dos.h>
    
    
    // D E F I N E S  ////////////////////////////////////////////////////////////
    
    #define MODE13H             0x13
    #define TEXT_MODE           0x03
    
    // P R O T O T Y P E S ///////////////////////////////////////////////////////
    
    void Video_Mode(int vmode);
    int  Create_Offscreen_Buffer(int width, int height);
    void Show_Offscreen_Buffer(void);
    void Delete_Offscreen_Buffer(void);
    void Fill_OBuffer(int color);
    void Plot_PixelOB(int x, int y, unsigned char color);
    void Draw_Sprite(int x, int y);
    
    // G L O B A L S  //////////////////////////////////////////////////////////
    
    unsigned char far *video_buffer     = (char far *)0xA0000000L;
    unsigned char far *offscreen_buffer = NULL;
    int sprite_x, sprite_y;
    
    void main(void) {
      sprite_x = 0;
      sprite_y = 10;
    
      Video_Mode(MODE13H);
    
      if(!Create_Offscreen_Buffer(320, 200)) {
        Video_Mode(TEXT_MODE);
        printf("Not enough memory to create offscreen buffer.\n");
        printf("Exiting to DOS...\n");
        exit(1);
      }
    
      while(!kbhit()) {
        Fill_OBuffer(0);  // clear contents of double buffer
        sprite_x++;
        if (sprite_x >= 309) sprite_x = 0;
        Draw_Sprite(sprite_x, sprite_y);
        Show_Offscreen_Buffer();
      }
    
      Delete_Offscreen_Buffer();
      Video_Mode(TEXT_MODE);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // V I D E O _ M O D E                                                     //
    // Sets the video mode to the value in vmode.                              //
    /////////////////////////////////////////////////////////////////////////////
    
    void Video_Mode(int vmode) {
      asm mov ah, 0
      asm mov al, BYTE PTR vmode
      asm int 10h
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // C R E A T E _ O F F S C R E E N _ B U F F E R                           //
    // Creates an offscreen buffer of the specified size.                      //
    /////////////////////////////////////////////////////////////////////////////
    
    int Create_Offscreen_Buffer(int width, int height)  {
      // allocate enough memory to hold the double buffer
      if ((offscreen_buffer = (unsigned char far *)farmalloc((unsigned int)width * (height + 1)))==NULL)
         return 0;
      // fill the buffer with black
      _fmemset(offscreen_buffer, 0, width * height);
      return 1;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // S H O W _ O F F S C R E E N _ B U F F E R                               //
    // Copies the contents of the offscreen buffer to the video buffer.        //
    /////////////////////////////////////////////////////////////////////////////
    
    void Show_Offscreen_Buffer(void)  {
      char far *buffer = offscreen_buffer;
      int buffer_size = 320*200/2;
      // this functions copies the double buffer into the video buffer
      asm   push ds               ; //save DS on stack
      asm   mov cx,buffer_size    ; //this is the size of buffer in WORDS
      asm   les di,video_buffer   ; //es:di is destination of memory move
      asm   lds si,buffer         ; //ds:si is source of memory move
      asm   cld                   ; //make sure to move in the right direction
      asm   rep movsw             ; //move all the words
      asm   pop ds                ; //restore the data segment
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // D E L E T E _ O F F S C R E E N _ B U F F E R                           //
    // Deletes the offscreen buffer.                                           //
    /////////////////////////////////////////////////////////////////////////////
    
    void Delete_Offscreen_Buffer(void)  {
      // this function free's up the memory allocated by the double buffer
      if (offscreen_buffer) farfree(offscreen_buffer);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // F I L L _ O B U F F E R                                                 //
    // Fills a 64K offscreen image buffer with the specified color.            //
    /////////////////////////////////////////////////////////////////////////////
    
    void Fill_OBuffer(int color) {
      _fmemset(offscreen_buffer, color, 64000);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // P L O T _ P I X E L O B                                                 //
    // Plots a pixel to the offscreen buffer.                                  //
    /////////////////////////////////////////////////////////////////////////////
    
    void Plot_PixelOB(int x, int y, unsigned char color) {
      offscreen_buffer[((y<<8) + (y<<6)) + x] = color;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // D R A W _ S P R I T E                                                   //
    // Draws a 10x10 box as an example sprite                                  //
    /////////////////////////////////////////////////////////////////////////////
    
    void Draw_Sprite(int x, int y) {
      int i, j;
      for (i=0; i<=10; i++) {
        for (j=0; j<=10; j++) {
          Plot_PixelOB(i+x, j+y, 11);
        }
      }
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,255

    Re: How to get the following Mode 13 H C Code to work in Visual Studio 2015 Pro ???

    This will compile for 32 bit VS2015. However, in-line assembler is not supported for 64 bit (but 32 bit code will run on 64 bit Windows). Whether the code will actually do what is expected........

    Code:
    // I N C L U D E S ///////////////////////////////////////////////////////////
    
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include <dos.h>
    
    
    // D E F I N E S  ////////////////////////////////////////////////////////////
    
    #define MODE13H             0x13
    #define TEXT_MODE           0x03
    
    // P R O T O T Y P E S ///////////////////////////////////////////////////////
    
    void Video_Mode(int vmode);
    int  Create_Offscreen_Buffer(int width, int height);
    void Show_Offscreen_Buffer(void);
    void Delete_Offscreen_Buffer(void);
    void Fill_OBuffer(int color);
    void Plot_PixelOB(int x, int y, unsigned char color);
    void Draw_Sprite(int x, int y);
    
    // G L O B A L S  //////////////////////////////////////////////////////////
    
    unsigned char  *video_buffer = (unsigned char *)0xA0000000L;
    unsigned char *offscreen_buffer = NULL;
    int sprite_x, sprite_y;
    
    void main(void) {
    	sprite_x = 0;
    	sprite_y = 10;
    
    	Video_Mode(MODE13H);
    
    	if (!Create_Offscreen_Buffer(320, 200)) {
    		Video_Mode(TEXT_MODE);
    		printf("Not enough memory to create offscreen buffer.\n");
    		printf("Exiting to DOS...\n");
    		exit(1);
    	}
    
    	while (!_kbhit()) {
    		Fill_OBuffer(0);  // clear contents of double buffer
    		sprite_x++;
    		if (sprite_x >= 309) sprite_x = 0;
    		Draw_Sprite(sprite_x, sprite_y);
    		Show_Offscreen_Buffer();
    	}
    
    	Delete_Offscreen_Buffer();
    	Video_Mode(TEXT_MODE);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // V I D E O _ M O D E                                                     //
    // Sets the video mode to the value in vmode.                              //
    /////////////////////////////////////////////////////////////////////////////
    
    void Video_Mode(int vmode) {
    	_asm mov ah, 0
    		_asm mov al, BYTE PTR vmode
    		_asm int 10h
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // C R E A T E _ O F F S C R E E N _ B U F F E R                           //
    // Creates an offscreen buffer of the specified size.                      //
    /////////////////////////////////////////////////////////////////////////////
    
    int Create_Offscreen_Buffer(int width, int height) {
    	// allocate enough memory to hold the double buffer
    	if ((offscreen_buffer = (unsigned char *)malloc((unsigned int)width * (height + 1))) == NULL)
    		return 0;
    	// fill the buffer with black
    	memset(offscreen_buffer, 0, width * height);
    	return 1;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // S H O W _ O F F S C R E E N _ B U F F E R                               //
    // Copies the contents of the offscreen buffer to the video buffer.        //
    /////////////////////////////////////////////////////////////////////////////
    
    void Show_Offscreen_Buffer(void) {
    	unsigned char *buffer = offscreen_buffer;
    	short int buffer_size = 320 * 200 / 2;
    	// this functions copies the double buffer into the video buffer
    	_asm   push ds; //save DS on stack
    	_asm   mov cx, buffer_size; //this is the size of buffer in WORDS
    	_asm   les di, video_buffer; //es:di is destination of memory move
    	_asm   lds si, buffer; //ds:si is source of memory move
    	_asm   cld; //make sure to move in the right direction
    	_asm   rep movsw; //move all the words
    	_asm   pop ds; //restore the data segment
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // D E L E T E _ O F F S C R E E N _ B U F F E R                           //
    // Deletes the offscreen buffer.                                           //
    /////////////////////////////////////////////////////////////////////////////
    
    void Delete_Offscreen_Buffer(void) {
    	// this function free's up the memory allocated by the double buffer
    	if (offscreen_buffer) free(offscreen_buffer);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // F I L L _ O B U F F E R                                                 //
    // Fills a 64K offscreen image buffer with the specified color.            //
    /////////////////////////////////////////////////////////////////////////////
    
    void Fill_OBuffer(int color) {
    	memset(offscreen_buffer, color, 64000);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // P L O T _ P I X E L O B                                                 //
    // Plots a pixel to the offscreen buffer.                                  //
    /////////////////////////////////////////////////////////////////////////////
    
    void Plot_PixelOB(int x, int y, unsigned char color) {
    	offscreen_buffer[((y << 8) + (y << 6)) + x] = color;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // D R A W _ S P R I T E                                                   //
    // Draws a 10x10 box as an example sprite                                  //
    /////////////////////////////////////////////////////////////////////////////
    
    void Draw_Sprite(int x, int y) {
    	int i, j;
    	for (i = 0; i <= 10; i++) {
    		for (j = 0; j <= 10; j++) {
    			Plot_PixelOB(i + x, j + y, 11);
    		}
    	}
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.3)

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: How to get the following Mode 13 H C Code to work in Visual Studio 2015 Pro ???

    I really don't think that VGA Mode 13 will work under windows. That kind of code was used in DOS days, which allowed you access to video memory directly, and was located at 0xA0000000 address. Windows does not allow you to access memory like that directly and pretty much it's all virtualized. I would make educated guess, that you most likely could use Dos Box (emulator for older DOS versions), and run it under it's environment. I do not know how the memory would work, since in old DOS you were limited to 640k. Granted, there was a concept of high and extended memory to break 640k barrier, but some of those options required to link with specific library or/and load resident drivers.

    IOW, it may be not worth to try to make that work and most likely still be cumbersome to use or setup correct environment. You could use windows blit surfaces for example,, where you would have some block of memory, which gets modified by you, and then blit it to the screen (win32 api's). Another option is to use something like DirectX or OpenGL.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to get the following Mode 13 H C Code to work in Visual Studio 2015 Pro ???

    Just to add what's already said by STLDude, I think it's "safe" to assume that everything related to DOS irqs are obsolete these days. The time of DOS was a fun time but now it's gone...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: How to get the following Mode 13 H C Code to work in Visual Studio 2015 Pro ???

    Hi ..

    I have used such type of code on DOS with 486. C Line and graphics routines were taking too much time.
    So we used Assemble Routines for graphics calls. But with Windows and faster CPU s we are happy with graphics
    lib .. GDI + , direct x etc

    so you can translate the algorithm .. not code line by line..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)