3
\$\begingroup\$

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ A whole game in 2 pages? I doubt that. And most APIs come with some line drawing function. Oh, and looking at OpenGL/DirectX is veering wildly away from 'shortest way'. \$\endgroup\$ Nov 3 '10 at 17:49
  • \$\begingroup\$ Shortest way: printf("+----_____\n\ \ \ \ \ \ \ \ \ \ ---+\n"); \$\endgroup\$ Nov 21 '12 at 20:54
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ thanks! After visiting wikipedia for GLUT, I found SDL. It seems more appealing to me. However, it would be nice to hear some opinions on the matter since I have no idea which is better. \$\endgroup\$
    – Mike
    Nov 2 '10 at 22:44
  • \$\begingroup\$ I found this: gamedev.net/community/forums/topic.asp?topic_id=444230 . It talks about the pros and cons of SDL vs GLUT vs GLFW vs Native. \$\endgroup\$
    – Nailer
    Nov 2 '10 at 22:54
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ SDL creates a window for you - different code depending on the platform, but the same basic functionality. Basic 2D graphics are supported - blitting is easy enough, and IIRC there's a simple line function. File formats need an extra library (SDL_Image?) with dependencies on libpng etc. Sound is a pain unless you add SDL_Mixer to the mix - simply getting multiple concurrent sound effects could overflow your two-pages budget. It's very easy to code for SDL, but not so easy to get all the libs and dependencies set up in a working build environment - at least not on Windows. \$\endgroup\$
    – user8524
    Nov 12 '11 at 3:01
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Here's a basic GLUT app that draws lines

#include <gl/glut.h>
#include <math.h>

void render()
{
  glClear( GL_COLOR_BUFFER_BIT );

  glBegin(GL_LINES);
  for( double i = 0 ; i < 2*3.14159; i+=0.01 )
  {
    double value1 = cos(i) ;
    double value2 = sin(i) ;
    glColor3d( fabs(value1), fabs(value1)*fabs(value2), fabs(value2) );
    glVertex2d( .2*value1*value2,.4*value2 ) ;
    glVertex2d( value1, value2 ) ;
  }
  glEnd() ;

  glutSwapBuffers() ;
}

int main( int argc, char** argv )
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(120,260);
  glutInitWindowSize(600,600); 
  glutCreateWindow("GLUT Window");

  glutDisplayFunc( render ); 
  glutMainLoop();
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .