What is the easiest and shortest way to draw a 2d line in c/c++?

Mike
  • What is the easiest and shortest way to draw a 2d line in c/c++? Mike

    I am fairly new to c/c++ but I do have experiance with directx and opengl with java and c#. My goal is to create a 2d game in c with under 2 pages of code. Most of what I have seen requires 3 pages of code to just get a window running.

    I would like to know the shortest code to get a window running where I can draw lines. I believe this can be done in less lines with opengl versus directx. Is there maybe an api or framework i can use to shorten it more?

    Also, it would be nice if the solution were cross platform compatible.

  • GLUT might be what you're looking for. It is a cross platform Open GL wrapper that helps setting up the basics.

    Hope this helps.

  • Also look at SDL. A good wrapper multiplatform to Directx (on Win)and OpenGl (on Linux and also Win ..). I'm using SDL and... it is easy to understand for a beginner!

    For Sdl exists a binding to C# and Java. For C# is called. SDL.NET

  • http://www.sfml-dev.org/ <- Making window, and drawing lines is very easy

  • You might want to use Cairo to draw 2d graphics on any backend. You can combine it with SDL or OpenGL or GTK, as display-backend, or with .png or .svg or .pdf as file-backend, too.

  • Here is an example program that draws a line to a window using the cross platform library Qt. Includes have been omitted.

    struct MyWindow: public QMainWindow
    {
      Q_OBJECT
    
      MyWindow(QWidget *parent = 0, Qt::WFlags flags = 0){ui.setupUi(this);}
      ~MyWindow(){}
    
      void paintEvent(QPaintEvent *)
      {
        QPainter painter(this);
        painter.setPen(Qt::blue);
    
        QLineF line(10.0, 80.0, 90.0, 20.0);
        painter.drawLine(line);
      }
    
      Ui::MyWindow ui;
    };
    
    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);
      MyWindow w;
      w.show();
      return a.exec();
    }
    

    Note that this example does not use OpenGL. To render to OpenGL using the same draw calls simply derive a class from QGLWidget.

Tags
c++ opengl directx
Related questions and answers
  • I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why. If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates? // a struct for the vertex shader return value struct VSOut { float4 Col

  • I would have thought that if the object is on-screen that this function should return screen coordinates. When used in conjunction with the directX draw text function, it works fine. Textual overlays... = xyCoords.y; m_font->DrawTextA(NULL, stateStream.str().c_str(), -1, &rct, DT_LEFT|DT_NOCLIP , D3DXCOLOR(1,0,0,1)); This all works perfectly. Where am I going wrong with the first bit of code...() will return a vector3 that has a 0 z component and x and y values from about 2 to roughly 33. Now how on earth can me with a calculator be better than the computer? what am I missing? 0.0f / 720.0f) * 2.0f

  • from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've... in the main function * not Finally.. but i believe that te numb_coll problem has something to do with the time and framrate. When running full speed i quickly die upon entering a wall as numb_coll...}; // for the old/new screen command // Some nCurses setup int r = 0, c = 0; // current row and column (upper-left is (0,0)) const int nrows = 56, // number of rows in window ncols = 79

  • version of both. This is so the permanent ones can just be loaded in and are always drawn, and the temporary ones only get drawn for a frame. This way I can create moving lines for debugging, like... it has exposed a basic gap in my knowledge about what some of these lines of code do. should I just populate one big list from all the vectors and push that through? some need to be drawn... it all separately? Basically how would I extend this code to include more vectors of vertices? if (temporary2DVerts.size() > 0) { // create the vertex buffer and store the pointer into pBuffer

  • NEHE'S TUT glBegin (GL_TRIANGLES); // Tell OpenGL What We Want To Draw for(int i = 0; i < numTriangles; i++) { MD2Triangle* triangle...I am quite new to OpenGL, I have managed after long trial and error to integrate Nehe's Cel-Shading rendering with my Model loaders, and have them drawn using the Toon shade and outline...); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i

  • better. I would also like compatability, If Theres one that fits DirectX(at least 9) and OpenGL, then that would be good) 2D Graphics(I liked SFML, so it its possible to get something that works with SFML..., so I planned to start over. The libraries that I have used are SFML, OpenGL, and Bullet Physics, along with GLM to load the models. But the problem is that the 3D graphics arent working with the Physics correctly... So my main question is: What would be a good combination of libraries to make an online game with? Im sure that many people have good combinations of libraries for making a game

  • I'm writing a simple class to draw all the debugging lines I have in my scene at once. The code in the draw loop is this so far: (If I put for example, 2 instead of temporary2DVerts.size() where I have marked with //THIS LINE, the code works fine.) when I run the code below the line breaks //HERE. Access violation reading location 0x00000000. seems like the create buffer line is not working, but why? what's the solution? D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(VERTEX) * temporary2DVerts.size();// THIS LINE bd.BindFlags = D3D10_BIND_VERTEX_BUFFER

  • I'm not asking for code or anything, just some advice on what to google, because I have no idea what to call this process. I made a video player (with full controls) with python and DirectX and would like to just simply import the rendering of the display into a c++ DirectX fullscreen app. That way, it can be customizable to the user. Any advice is greatly appreciated.

  • I already know many computers today have a GPU that supports DirectX and OpenGL. But what about computers that don't? Can I write a simple 2D game that does not rely on the GPU, so that it works even on older machines? Is there a cross-platform C or C++ game library that will let me do this? Ideally, such a library would let me toggle hardware acceleration with very few lines of code.