How are APIs made, and how do they work with programming languages?

Started by
8 comments, last by lmbarns 11 years, 11 months ago
I've wondered this for time.

Who, or specifically how and where, are the APIs written, from what, to what, and how can they work with languages till the very end of code compilation/assembling?

Are they libraries with specific low-level functions that are written from any other language that perform low-level calls on .dll files to change aspects, such as Window display, etc.?

Do they go lower-level than Windows or the OS itself's natural dependency, or that depends?

For example, does SDL perform low-level functions beneath the Windows API, or just for its own client window?

Very confused with this, and giving some suggestions. No harm, no foul, and not trolling either.
Advertisement
It's a fairly vague term. They're often, but not always, libraries, and the level of abstraction depends on what they're doing. e.g. Something like the Twitter API might just be a normal HTTP request for a JSON object, whilst something like Direct3D is a set of headers and compiled libraries that interact closely with the driver. In between, an API for another piece of desktop software (Word or Excel) might just be a library exposing its plotting functionality or file format. I don't know SDL in detail, but since it's cross-platform, I'd expect it's written on top of OpenGL and some library like GTK+, possibly using conditional compilation to port between platforms.
[TheUnbeliever]
I don't think you entirely understood what I was asking in great detail.

I also don't know what you meant exactly by saying "[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]written on top of OpenGL" either.[/background]

[/font]


Who, or specifically how and where, are the APIs written, from what, to what

An API is just a library of code, and in that respect it's not really special or different than any other piece of software other than the fact that it may (but may not be!) be at a lower level than you would normally want to work at when writing an application. An API generally provides an interface or abstraction of a lower level system, whether this is another software library or working more directly with a piece of hardware.

The programmer creates source-code just like you do yourself when writing a program, and then compile it just like you would -- although they will probably choose to produce a dll or other library file format rather than an executable -- this is just a different compiler option, and might in some cases require some specific coding styles.


For example, does SDL perform low-level functions beneath the Windows API

SDL uses the Windows API (this is what TheUnbeliever means by "written on top of"), and other lower level APIs.
Rather than needing to know all the Windows API calls necessary to set up and control a window, you use the simpler SDL functions. This is what I mean when I say they provide an "interface".
SDL can also be used on operating systems other than Windows, and on those other systems SDL will use an API appropriate to that OS rather than the Windows API -- but as a programmer using SDL you don't need to know or care about that difference, you just call the same SDL functions. This is what I mean when I say an API provides an "abstraction".

Is that any clearer?

- Jason Astle-Adams

Yes, thanks heaps!

Very clear.

But can I ask something else?

How, or specifically under what circumstances of implementation, are lower level functions, codes, etc. applied?

I am a bit cloudy there.

Like how SDL works with WinAPI ... how does it do it? I mean to add, I understand VERY little Windows API programming, but what I don't understand is how it exactly works beneath just typing in things, and I really want to know how, but reading the header files is like Hieroglyphics, and doesn't help me.

I just feel that by knowing this it would help me get the bigger picture of API use, specifically the only one I have managed to use correctly and have succeeded in(SDL).

This is, to also add, why I feel that learning some lower-level codes, how they work, how they work with each other, and how they work in general, would help me a bunch as I finally start stepping up in programming, designing and completing my first ever game program, etc.

So if you could wire this together in some sense, any sense, I'd appreciate your efforts.
APIs tend to simplify code. A simple API function call (or in this case a constructor) to initialize a window may be:
programWindow myWindow(800, 600, 32);
Passing in just those parameters.

But if you want to do it without the API you will have to call several different functions to set up your window, say one setting the width and height, one setting what resolution you want (24bit, 32bit, etc) and then you may have to deal with various window handles.

The API may also take care of catching window events for you, and all you do is ask for them, while without the API you have to register to receive the events, then make sure to check for them.

A simple API function call (or in this case a constructor) to initialize a window may be:
programWindow myWindow(800, 600, 32);
Passing in just those parameters.


I understand, but what do you mean by "(or in this case a constructor)"?

Being very unfriendly with C++'s OOP, I can't say for sure that I get that.

Although most of what you say clarifies it, I still wish to understand how exactly it does it for Windows API.
You'll need to learn more about programming in general -- and in this specific case classes in C++ -- to understand properly. I'd suggest working through a good book or a set of online tutorials on the basics of C++. A lot of the questions you're having should then already be answered, and you'll be much better equipped to understand the answers to those you might still have.

- Jason Astle-Adams

I apologize if I added any extra confusion.
In simplest and broad terms isn't an API basically a collection/library of public methods(or externally accessible)? With emphasis on abstraction/OO? I guess an interface could expose more than public methods?

Thinking of common web API's, Google charts is a simple example, they have elaborate functions to do cool stuff internally that you don't need to know about, you simply connect and pass in the information you want it to perform cool actions on, and it gives you back the result of the information you passed in, in a dynamic or interactive format.

Jquery for example is simply javascript that you access from other javascript files. Instead of writing your own function to make an element draggable, you simply call a function premade in jquery(plain javascript) which takes care of detecting mouse click, monitoring drag coordinates on an element, etc. you just worry about what element you want to be draggable.

This topic is closed to new replies.

Advertisement